hook

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package hook holds what a grelot hook decides before it asks the server: the fields of the host's hook input, which session the hook acts for, and the per-session marks it keeps between runs.

Index

Constants

View Source
const (

	// MarkOnce is a role's first print for a session, for a role under once.
	MarkOnce = "once"
	// MarkHostHooked is a hook run of a host on this machine, keyed by the
	// provider's name, and MarkSessionHooked one of a session, keyed by the
	// session as the CLI sends it (<provider>:<id>).
	MarkHostHooked    = "hooks"
	MarkSessionHooked = "hooked"
)

The kinds of mark a hook keeps.

Variables

This section is empty.

Functions

func OneShot

func OneShot(p *provider.Provider, lookup func(string) (string, bool), ancestors func() []proctree.Process) bool

OneShot reports whether the hook runs in a one-shot run of p's host, by p's one_shot: one of its variables holding a listed value, or a listed argument, in one of provider.OneShot.Args' forms, on a process of the host's own run of ancestors (proctree.Host). ancestors is called only when no variable matches. It answers false when neither is seen, so an ancestor whose arguments cannot be read keeps the run interactive.

Types

type Class

type Class struct {
	// Stdin is the hook input's session id, "" when the provider names
	// no session.stdin or the input holds none.
	Stdin string
	Kind  Kind
	// Fork marks a Child whose record names a parent: a second chat beside
	// it, which gets the fork's one line.
	Fork bool
}

Class is a hook's classification.

func Classify

func Classify(p *provider.Provider, in Input, agentID string, sh Shell, marks *Marks) Class

Classify decides whose hook in is. agentID is the role's stdin path of the subagent id. A SameAgent result records its parent as replaced in marks, so the old session's hooks go quiet from then on.

type Input

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

Input is a hook's stdin, one JSON object.

func ReadInput

func ReadInput(r io.Reader) (Input, error)

ReadInput reads r as one JSON object. Empty input is an empty object, so a host that sends nothing still gets its environment session's hook.

func (Input) Matches

func (in Input) Matches(when map[string][]string) bool

Matches reports whether every path of when holds one of its values, the role filter a provider file spells as when.

func (Input) String

func (in Input) String(path string) string

String is the value at a dotted field path, "a.b", as text: a string as is, a number or boolean as JSON spells it, "" for a missing path, null, an object or an array.

type Kind

type Kind int

Kind is whose hook this is.

const (

	// SameAgent is the same agent going on in a new session: it runs as
	// parent does.
	SameAgent Kind
	// Child is a subagent, a workflow step or a fork beside a parent that
	// goes on: it never acts for the parent.
	Child
	// Replaced is a session a same-agent fork has replaced: it prints
	// nothing.
	Replaced
)

type Marks

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

Marks are small per-session files under the client's state directory. Every write is best effort: a failed one never fails a hook.

func NewMarks

func NewMarks(configDir string, now func() time.Time) *Marks

NewMarks keeps marks under configDir's grelot/state/marks folder.

func ReadMarks

func ReadMarks(configDir string, now func() time.Time) *Marks

ReadMarks reads the marks NewMarks keeps and never writes one: the marks of a client with no usable config, which leaves no state behind.

func (*Marks) Has

func (m *Marks) Has(kind, key string) bool

Has reports whether the mark is set.

func (*Marks) Set

func (m *Marks) Set(kind, key string)

Set sets the mark to now and drops the marks not set within markLife. ReadMarks' Set writes nothing.

func (*Marks) Touch

func (m *Marks) Touch(kind, key string, every time.Duration)

Touch sets the mark unless it was set within every, so a caller that runs often writes about once per every, and a mark it keeps touching more often than markLife is never pruned.

func (*Marks) When

func (m *Marks) When(kind, key string) (time.Time, bool)

When is the time the mark was last set. A mark set over markLife ago is absent, whether or not a write has pruned it yet.

type Shell

type Shell struct {
	Lookup func(name string) (string, bool)
	Home   func() (string, error)
	// Session is the environment's host session id, without the provider.
	Session string
}

Shell is what classification reads of the hook's process.

Jump to

Keyboard shortcuts

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