gates

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 14 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 gates holds every decision waiting on the user: it opens a gate, resolves it only through the user API or a trusted policy rule, and applies the one consequence its kind names.

Index

Constants

View Source
const RuleWindow = "window"

RuleWindow is the Rule of a gate its passed Deadline resolved to its Default.

Variables

View Source
var (
	// ErrRefused is a resolution the gate does not accept: by an agent through
	// a session the user does not drive, by a plugin, by a rule the trusted
	// policy does not hold, or by a window that has not passed.
	ErrRefused = errors.New("the gate refuses the resolution")
	// ErrMoved is a pick of a draft at a rev its gate no longer waits on, a kind of ErrRefused: the resolver
	// saw another version of the draft.
	ErrMoved = errors.New("the draft is at another rev than the one picked")
	// ErrIntent is an intent the gate does not offer, or one gates.toml does
	// not declare for it.
	ErrIntent = errors.New("the gate offers no such intent")
	// ErrNoDef is a gate gates.toml does not declare, by name or by kind.
	ErrNoDef = errors.New("gates.toml declares no such gate")
)
View Source
var (
	ErrNoGate   = errors.New("no such gate")
	ErrResolved = errors.New("the gate is resolved already")
)

Functions

This section is empty.

Types

type Begun

type Begun interface {
	// Begin marks resolved gate id's consequence run, and begun until End.
	Begin(ctx context.Context, id int64) error
	End(ctx context.Context, id int64) error
	// Begun are the resolved gates whose consequence began and never ended.
	Begun(ctx context.Context) ([]gate.Gate, error)
}

Begun is optional on a Store: it keeps an at-most-once consequence begun until it ends, so one a stopped daemon cut can be reported.

type Decider

type Decider interface {
	Decide(ctx context.Context, g gate.Gate) error
}

Decider applies the operations of a resolved proposal gate.

type Decision

type Decision struct {
	Mode   gate.Mode
	Window time.Duration
	Rule   string
}

Decision is how an approval decides a gate: its Mode, a delay's Window, and Rule, the rule that decided, which a gate it releases records.

type Def

type Def struct {
	Name    string
	Kind    gate.Kind
	Label   string
	Intents []Intent
	// EndsTask is the category, done or dropped, a task opened only to hold gates ends in once the gate closes
	// with a status and none of its gates is open; a status it leaves out leaves the task open.
	EndsTask map[gate.Status]task.Category
	// ClosesWith is the intent, one that refuses the gate, the gate closes with once its task ends; "" leaves it
	// open.
	ClosesWith string
}

Def is a gate the profile's gates.toml declares: its kind, its words and its intents in the order clients offer them.

type Deliverer

type Deliverer interface {
	Deliver(ctx context.Context, g gate.Gate) error
}

Deliverer delivers a resolved question or post gate's answer to its session.

type Deps

type Deps struct {
	Store   Store
	Reader  Reader
	Profile Profile
	// Tasks gives a gate the task of its unit; nil leaves it with none.
	Tasks Tasks
	// Policy decides a gate Open is given no policy for, and is the one a
	// resolution naming a rule is checked against; nil holds every gate.
	Policy Policy
	Clock  port.Clock

	Publisher Publisher
	Releaser  Releaser
	Deliverer Deliverer
	Decider   Decider
	Unblocker Unblocker
	// Events records a resolved step gate; nil records none.
	Events Events
	// Sessions says which sessions the user drives; nil refuses every agent.
	Sessions Sessions
	// Options report the draft an answer to a question picks, which ApproveOption approves before it answers
	// the question; nil approves none.
	Options Options

	Log func(format string, args ...any)
}

Deps are what the service reads, writes and applies. Every consequence port is optional: a kind whose port is nil resolves with no consequence.

type Edit added in v0.7.0

type Edit func(ctx context.Context, drafted gate.Gate) (gate.Gate, error)

Edit is a person's change of an option's draft, made as they pick the option: it revises the draft drafted holds, which supersedes drafted, and returns the open gate the revision waits on.

type Events

type Events interface {
	RecordEvents(ctx context.Context, evs ...port.Event) error
}

Events records the event a resolved step gate is, which workflows trigger on.

type HeldTasks added in v0.4.0

type HeldTasks interface {
	EndHeld(ctx context.Context, id task.ID, c task.Category) error
}

HeldTasks is optional on Tasks: ends task id, one Tasks opened only to hold gates, into category c once its last gate closed; a task opened otherwise, or ended already, is left as it is.

type Intent

type Intent struct {
	Name   string
	Status gate.Status
	Answer bool
}

Intent is what a gate is resolved with: the status gates.toml gives it, and whether it takes text.

type Once

type Once interface {
	Once(g gate.Gate) bool
}

Once is optional on a consequence port: a gate it answers true for is marked run before its consequence runs, so a daemon that stops during the consequence never runs it again, and may leave it not run. For a consequence that acts in the user's name and keeps no record a second run could read, such as a round or a verb.

type Options added in v0.5.0

type Options interface {
	OptionDraft(ctx context.Context, question gate.Gate, r Resolution) (drafted gate.Gate, rev int64, ok bool, err error)
	// Pinning is the question gate, open or answered while its message still settles, whose message pins draft
	// gate drafted's draft, in an option or its confirm, the answer that picks that pin and the rev it pinned;
	// false when no open message pins it. question is zero while the message's question gate is not opened yet.
	Pinning(ctx context.Context, drafted gate.Gate) (question gate.Gate, answer string, rev int64, ok bool, err error)
}

Options report the pin an answer to a question picks, in the question its person saw: the gate of the draft the option points to and the rev it pinned; false for an answer that picks none. Whether the answer approves that draft is the gates service's alone (ApproveOption).

type OwnTasks

type OwnTasks interface {
	ReopenOwn(ctx context.Context, u unit.Unit) (task.ID, bool, error)
}

OwnTasks is optional on Tasks: the unit's own task, such as a pull request's, which a gate on the unit goes to whatever its state, reopened when it is done, so the unit keeps one task.

type Policy

type Policy interface {
	Decide(ctx context.Context, g gate.Gate) (Decision, error)
}

Policy is a trusted approval: the profile's and the configuration's [gates.approval], never an agent's or a thread's text.

func WorkflowPolicy added in v0.4.0

func WorkflowPolicy(w workflow.Workflow, facts workflow.Facts, gateFacts func(gate.Gate) workflow.Facts, now time.Time) Policy

WorkflowPolicy is the approval of the gates workflow w opens on a unit whose facts are facts: the [gates.approval] of the gate's kind, w's run approval for a plan gate, its first rule that holds over facts and gateFacts of the gate at now, else its mode and window. A gate of a kind w approves none of waits.

type Profile

type Profile interface {
	Gate(name string) (Def, bool)
	FirstOf(k gate.Kind) (Def, bool)
}

Profile is the gates the profile declares, as loaded at each call.

type Publisher

type Publisher interface {
	Publish(ctx context.Context, g gate.Gate) error
}

Publisher publishes the draft of a resolved draft gate.

type Query

type Query struct {
	Kind     gate.Kind
	Name     string
	Task     task.ID
	Unit     unit.Unit
	Statuses []gate.Status
	// Subjects keeps the gates whose Subject is one of them; empty matches every subject.
	Subjects []string
}

Query narrows Where: a zero field matches every gate, but Statuses, which matches the open gates when empty.

type Reader

type Reader interface {
	// Get is gate id; ErrNoGate when there is none.
	Get(ctx context.Context, id int64) (gate.Gate, error)
	// OfTask are task id's gates, open and closed.
	OfTask(ctx context.Context, id task.ID) ([]gate.Gate, error)
	// Where are the gates matching q.
	Where(ctx context.Context, q Query) ([]gate.Gate, error)
}

Reader reads the gates, open and closed. Its lists are ordered by ID, oldest first; a list nothing matches is empty.

type Releaser

type Releaser interface {
	Release(ctx context.Context, g gate.Gate) error
}

Releaser releases the plan item of a resolved plan gate.

type Resolution

type Resolution struct {
	Status  gate.Status
	Intent  string
	Answer  string
	By      task.Actor
	Rule    string
	Session string
	// contains filtered or unexported fields
}

Resolution is how a gate closed: Intent is the intent a person or a rule chose and Status the one gates.toml gives it, Answer a question's text, By who resolved it, Rule the policy rule that released it ("window" for a deadline's default), "" for a person. Session is the session an agent resolves it through, which the service checks the user drives.

type Service

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

Service opens the gates and is the only code that resolves one.

func New

func New(d Deps) *Service

func (*Service) ActingFor added in v0.4.0

func (s *Service) ActingFor(ctx context.Context, id task.ID) ([]gate.Gate, error)

ActingFor are the open gates that would act in task id's name: those of kind proposal, plan or step on its own unit, and the proposals on any task whose proposed verb names id in task.ArgTask.

func (*Service) ApproveOption added in v0.5.0

func (s *Service) ApproveOption(ctx context.Context, question gate.Gate, r Resolution, drafted gate.Gate, rev int64) error

ApproveOption approves draft gate drafted, which the option a person (or an agent through a session the user drives) picked points to at rev, the rev the option pinned, then resolves question gate question with r, the option picked (Intent, Answer) and who picked it (By, Session). It reads both gates again from the store first, trusting neither value the caller passes, and resolves nothing when drafted is no longer open (or approved already by r's resolver) at rev, is on another unit than the question, or came from another session than the question's, or when r is a rule's: each is ErrRefused. The draft is approved before the question is answered, so an answered question names the one draft its answer approved: a later call on an answered question completes only with that draft gate approved, and refuses any other draft gate, another option's or a revision's, with ErrRefused. A draft that loses its approval to a race (a revision) leaves the question open. Each step is idempotent, so a call cut short completes when it is made again with the same r.

func (*Service) Cut

func (s *Service) Cut() []gate.Gate

Cut are the resolved gates whose at-most-once consequence began and was cut by a daemon that stopped, so it may not have run, by ID.

func (*Service) Decides added in v0.5.0

func (s *Service) Decides(ctx context.Context, drafted gate.Gate) error

Decides refuses, with ErrRefused, a retry of the draft of draft gate drafted while a question decides it: an open question pins it, or one is about to open, or the question that pins it was answered with another option.

func (*Service) HoldsTask added in v0.4.0

func (s *Service) HoldsTask(ctx context.Context, id task.ID) (bool, error)

HoldsTask says whether an open gate of task id holds it open: one whose ClosesWith names one of its intents, a question to the person only the person, or the end of the task's unit, settles.

func (*Service) NextDeadline

func (s *Service) NextDeadline(ctx context.Context) (time.Time, error)

NextDeadline is the earliest Deadline of an open gate, zero when none has one.

func (*Service) Open

func (s *Service) Open(ctx context.Context, g gate.Gate, p Policy) (gate.Gate, error)

Open opens g under p, nil meaning the service's Policy. The gate takes its kind and intents from gates.toml: the gate g names, else the first of its kind. It takes the task of its unit, or a new capture task when no task holds it. A policy that releases it resolves it at once with its rule; a delay gives it a Deadline and the Default its approving intent; a dry_run rejects a draft gate and holds any other.

func (*Service) PickOption added in v0.7.0

func (s *Service) PickOption(ctx context.Context, id int64, r Resolution, rev int64, edit Edit) error

PickOption answers open question gate id with r, which picks an option pinning a held draft, approving that draft at rev, the rev the person saw, which may be newer than the one the option pinned, after edit (nil for none) changes it, all under the question's claim, as ApproveOption does. It refuses with ErrRefused an answer that picks no held draft, and a rev the draft no longer waits on: the person saw another version of it.

func (*Service) Resolve

func (s *Service) Resolve(ctx context.Context, id int64, r Resolution) error

Resolve closes open gate id with r. It accepts a person's intent, an agent's through a session the user drives, the Default of a gate whose Deadline passed with the rule window, and a rule the service's trusted Policy holds for the gate now, but for a draft a question pins, which only the pick approves; it refuses anything else, an unattended agent's or a plugin's above all. Status comes from the intent's declaration in gates.toml. It then applies the one consequence the gate's kind names.

func (*Service) SettleEnded added in v0.4.0

func (s *Service) SettleEnded(ctx context.Context) error

SettleEnded settles every task a closed gate whose status ends a held task was on, as SettleHeld does, so a task left open by gates that closed before their declaration ended it ends now.

func (*Service) SettleHeld added in v0.4.0

func (s *Service) SettleHeld(ctx context.Context, id task.ID) error

SettleHeld ends task id when Tasks opened it only to hold gates and none of them is open, in the category the last gate to close that names one ends it in; it settles a task whose gates closed before endHeld ran.

func (*Service) Supersede

func (s *Service) Supersede(ctx context.Context, id int64, by task.Actor) error

Supersede closes open gate id as superseded by by, its subject gone or replaced, with no consequence: the subject's owner settles the subject. It refuses an agent: no agent closes an open gate. A person supersedes a draft's gate only to replace it with its edit's.

func (*Service) SupersedeForAgent added in v0.5.0

func (s *Service) SupersedeForAgent(ctx context.Context, id int64, session string) (gate.Gate, error)

SupersedeForAgent closes open draft gate id as superseded because session, an agent, revised its draft, and returns the closed gate. It accepts any non-empty session: the outbox decides which agent may revise a draft, and stamps the closed gate's Sessions plus session on the gate it opens for the new rev. It runs no consequence, as Supersede does, and refuses every gate but a draft's.

func (*Service) TaskEnded added in v0.4.0

func (s *Service) TaskEnded(ctx context.Context, id task.ID, by task.Actor, session string, at time.Time) (err error)

TaskEnded settles each gate of task id that HoldsTask counts and that opened by at, when by ended the task, through session when an agent did: the user's end, a person's or an agent's through a session the user drives, closes it with its ClosesWith intent, which refuses the gate, in their name, and supersedes one whose subject the session's own turn produced; a plugin's or a workflow's end, which only the end of the task's unit makes, supersedes it as moot. It never approves or answers, refuses any other agent, and logs what it returns.

func (*Service) TaskOf added in v0.5.0

func (s *Service) TaskOf(ctx context.Context, g gate.Gate) (task.ID, error)

TaskOf is the task gate g is placed on when it opens: its own when it names one, else the one Open picks, which may be a capture task opened now.

func (*Service) Tick

func (s *Service) Tick(ctx context.Context, now time.Time) error

Tick resolves every open gate whose Deadline is not after now to its Default, with the rule window, but a draft gate a question pins, which it leaves to the question, then runs the consequence of each resolved gate whose consequence never ran, as when the daemon stopped between the gate's resolution and its consequence.

func (*Service) Withdraw added in v0.4.0

func (s *Service) Withdraw(ctx context.Context, id int64, reason string) error

Withdraw closes open step gate id as superseded by the workflow that opened it, whose subject is gone, and records gate.EventWithdrawn with reason on its task.

type Sessions added in v0.4.0

type Sessions interface {
	UserDriven(ctx context.Context, id string) (bool, error)
}

Sessions says whether the user drives an agent's session, by its stable id.

type Store

type Store interface {
	// Open stores g as open with a new ID and OpenedAt, and returns it.
	Open(ctx context.Context, g gate.Gate) (gate.Gate, error)
	// Resolve closes the open gate id with r and returns it: ErrNoGate when
	// there is none, ErrResolved when it is closed already. A Resolution whose
	// Status is open is refused.
	Resolve(ctx context.Context, id int64, r Resolution) (gate.Gate, error)
	// Due are the open gates whose Deadline is set and not after now.
	Due(ctx context.Context, now time.Time) ([]gate.Gate, error)
	// Applied marks resolved gate id's consequence as run; an open gate is
	// refused.
	Applied(ctx context.Context, id int64) error
	// Unapplied are the resolved gates whose consequence has not run: a
	// superseded gate and a recorded send have none.
	Unapplied(ctx context.Context) ([]gate.Gate, error)
}

Store writes the gates. Its lists are ordered by ID, oldest first; a list nothing matches is empty.

type Tasks

type Tasks interface {
	TasksOf(ctx context.Context, u unit.Unit) ([]task.Task, error)
	Open(ctx context.Context, r coretasks.OpenRequest) (task.Task, error)
}

Tasks finds the task a gate's unit is on, and opens one when none is.

type Unblocker

type Unblocker interface {
	Unblock(ctx context.Context, g gate.Gate) error
}

Unblocker unblocks the first run a resolved trust gate held.

Directories

Path Synopsis
Package gatestest holds an in-memory gates store, a recorder of the gates' consequences, and the suite every gates store passes.
Package gatestest holds an in-memory gates store, a recorder of the gates' consequences, and the suite every gates store passes.

Jump to

Keyboard shortcuts

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