outbox

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 15 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 outbox keeps the drafts the loop proposes and delivers them. Each draft waits on a draft gate, which holds the decision: the outbox posts a draft only once its gate approved it.

Index

Constants

View Source
const NotConfirmed = "not confirmed after a restart"

NotConfirmed is the Error of a draft Recover found in sending and Verify could not confirm.

Variables

View Source
var (
	ErrNotFound     = errors.New("no such draft")
	ErrConflict     = errors.New("the draft changed state")
	ErrDisabled     = errors.New("the draft's plugin is disabled or has no publisher")
	ErrDuplicate    = errors.New("an open draft of the same rule and key exists")
	ErrNeedsConfirm = errors.New("the draft may have been posted; retry needs confirm")
	ErrInvalid      = errors.New("invalid draft")
	ErrNoGates      = errors.New("the outbox has no gates to hold its drafts")
)
View Source
var ErrUnknownEffect = errors.New("no implementation of the effect")

ErrUnknownEffect is an effect a draft declares that has no implementation for its provider.

View Source
var ErrUnknownOrigin = errors.New("unknown proposer")

ErrUnknownOrigin is a draft's proposing intent, workflow or turn that the manifests or the book do not know.

Functions

func EvidenceOps

func EvidenceOps(d draft.Draft) []items.Op

EvidenceOps are the items ledger ops that record sent draft d on each of its items; the caller applies them with provenance items.Effects.

Types

type AppliedStore added in v0.2.0

type AppliedStore interface {
	// Unapplied are the sent drafts that declare effects and whose
	// EffectsAppliedAt is zero, oldest first.
	Unapplied(ctx context.Context) ([]draft.Draft, error)
	// MarkApplied stores at as the draft's EffectsAppliedAt.
	MarkApplied(ctx context.Context, id draft.ID, at time.Time) error
}

AppliedStore keeps when a sent draft's effects were applied.

type Applier added in v0.2.0

type Applier interface {
	Apply(ctx context.Context, d draft.Draft) error
	// Pending are the sent drafts whose effects are not applied yet.
	Pending(ctx context.Context) ([]draft.Draft, error)
}

Applier applies the Effects of a sent draft exactly once: each by its name, then the draft's EffectsAppliedAt, so a restart applies only those of drafts sent and not yet applied.

type Effect added in v0.2.0

type Effect func(ctx context.Context, d draft.Draft) error

Effect applies one declared effect of a sent draft.

type EffectApplier added in v0.2.0

type EffectApplier struct {
	Store   AppliedStore
	Effects EffectLookup
	Clock   port.Clock
}

EffectApplier is the Applier over a store and the effect implementations.

func (EffectApplier) Apply added in v0.2.0

func (a EffectApplier) Apply(ctx context.Context, d draft.Draft) error

Apply runs every effect d declares, in order, then marks d applied; a draft not sent, or already applied, is left as it is. When an effect fails nothing is marked, so the next Pending pass runs them all again: each effect is idempotent.

func (EffectApplier) Implements added in v0.2.0

func (a EffectApplier) Implements(p unit.ProviderID, name string) bool

func (EffectApplier) Pending added in v0.2.0

func (a EffectApplier) Pending(ctx context.Context) ([]draft.Draft, error)

type EffectLookup added in v0.2.0

type EffectLookup func(p unit.ProviderID, name string) (Effect, bool)

EffectLookup is the implementation of effect name for drafts of provider p; false when there is none.

type Effects

type Effects func(p unit.ProviderID) (port.Effects, error)

Effects returns the publishing capability of a provider; an error wrapping ErrDisabled when its plugin is off or has no publisher.

type Evidence

type Evidence func(ctx context.Context, d draft.Draft) error

Evidence receives a sent draft's evidence for its items (wired to the items ledger, op "evidence").

func EvidenceTo

func EvidenceTo(apply func(ctx context.Context, prov items.Provenance, ops []items.Op) error) Evidence

EvidenceTo is an Evidence that applies a sent draft's EvidenceOps through apply, with provenance effects.

type Facts added in v0.2.0

type Facts func(ctx context.Context, u unit.Unit, workflow string) (draft.Subject, error)

Facts reads the facts of unit u that a policy's rules read, scoped by workflow when it is set.

type Filter

type Filter struct {
	States    []draft.State
	Unit      *unit.Unit
	Task      string
	Rule, Key string
	// Workflow keeps the drafts whose Origin.Workflow is it; "" any.
	Workflow string
}

type Gates added in v0.3.0

type Gates interface {
	Open(ctx context.Context, g gate.Gate, p gates.Policy) (gate.Gate, error)
	Supersede(ctx context.Context, id int64, by task.Actor) error
	RecordSent(ctx context.Context, g gate.Gate) (gate.Gate, error)
	Where(ctx context.Context, q gates.Query) ([]gate.Gate, error)
}

Gates holds the decision each draft waits on: a draft gate whose Subject is the draft's ID. Its resolution reaches the outbox through Publish.

type Implementer added in v0.2.0

type Implementer interface {
	Applier
	Implements(p unit.ProviderID, name string) bool
}

Implementer is an Applier that says whether it has effect name for the drafts of provider p, so Propose refuses a draft it could never apply.

type Manifests

type Manifests interface {
	// DefaultApproval is gates.toml's default for a draft gate, which a
	// draft no manifest's block decides takes.
	DefaultApproval() draft.Approval
	// IntentApproval is the block of intent name, nil when it declares none; known is false for an unknown intent.
	IntentApproval(name string) (p *draft.Policy, known bool)
	// WorkflowApproval is the policy of the drafts of a turn workflow name
	// started, of an intent whose block is intent: the workflow's block, else
	// intent, with what the configuration lays over it; nil when none
	// applies. known is false for an unknown workflow.
	WorkflowApproval(name string, intent *draft.Policy) (p *draft.Policy, known bool)
	// Turn is the intent of turn seq in u's book and the workflow that started it; ok is false when the book has no such turn.
	Turn(ctx context.Context, u unit.Unit, seq int64) (intent, workflow string, ok bool, err error)
}

Manifests reads the [gates.approval] of the loaded intents and workflows for their draft gates, the profile's default for one, and the turns book.

type Pending added in v0.3.0

type Pending struct {
	Draft draft.Draft
	Gate  gate.Gate
}

Pending is a draft the Outbox shows, with its gate.

type Policies

type Policies interface {
	For(ctx context.Context, u unit.Unit, o draft.Origin) (draft.Policy, error)
}

Policies resolves the approval policy of a proposal on u whose Policy is nil.

type Proposal

type Proposal struct {
	Unit                   unit.Unit
	Items                  []string
	Task                   string
	Kind                   draft.Kind
	Target, Text, Reaction string
	// Replaced is the text a recorded edit replaced.
	Replaced string
	Origin   draft.Origin
	// Policy is the draft's approval policy; nil: resolved by
	// Service.Policies from Origin.
	Policy *draft.Policy
	// Subject is the subject the proposer read, whose facts the policy's
	// rules read; nil: Service.Facts reads the unit's.
	Subject *draft.Subject
	// Purpose and Delivery are the draft's shape; "" derives them from Kind
	// (draft.ShapeOf), and a set Purpose with no Kind derives Kind
	// (draft.KindOf).
	Purpose  string
	Delivery draft.Delivery
	// Effects are applied once the draft is sent, names of builtin.Effects.
	Effects []string
	// RepeatAfter is how long a settled draft of the same origin holds this
	// proposal from being proposed again; zero keeps Service.RepeatAfter.
	RepeatAfter time.Duration
	// Tool is the tool that made a post RecordSent records.
	Tool string
}

type Resolver

type Resolver struct {
	Manifests Manifests
}

Resolver is the Policies of the manifests, for turn and session drafts only: the proposing turn's workflow's policy laid over its intent's block, else the profile's default. The intent and workflow come from the turn in the book; an Origin that names others than the book's resolves to wait. Every other origin resolves to wait, so a rule's policy reaches a draft only through Proposal.Policy.

func (Resolver) For

func (r Resolver) For(ctx context.Context, u unit.Unit, o draft.Origin) (draft.Policy, error)

type Results added in v0.3.0

type Results interface {
	// AttachesPosts says whether the turns of intent attach their posts.
	AttachesPosts(intent string) bool
	Attach(ctx context.Context, tk task.ID, u unit.Unit) error
}

Results attaches to a task the unit of each post a turn on it makes.

type Service

type Service struct {
	Store Store
	// Gates holds the gate each draft waits on.
	Gates   Gates
	Effects Effects
	// Targets are the providers whose publishers PublisherOf asks to take a
	// draft's target.
	Targets  []unit.ProviderID
	Policies Policies // nil resolves every proposal to wait
	// Posts says whether a draft of a delivery reaches people, as the
	// plugins' manifests declare it, so a policy releases one only by a rule
	// that names that delivery; nil: every delivery does.
	Posts func(d draft.Delivery) bool
	Facts Facts // nil: a policy whose rules read the unit's facts resolves to wait
	// Broken names the workflows that failed to load; a draft of one whose
	// gate a rule or a window released fails, for a person to retry, until
	// it loads again.
	Broken   func() []string
	Evidence Evidence // nil none
	Changes  port.Changes
	Clock    port.Clock
	Log      func(format string, args ...any) // ids, states and kinds only; never text or tokens
	// OnSent runs after a draft is stored as sent and its Evidence call (last-nudge stamps, plugin bookkeeping).
	OnSent []func(ctx context.Context, d draft.Draft) error
	// RepeatAfter is how long a dry_run draft keeps an auto proposal of its key from being stored again;
	// zero keeps it for good.
	RepeatAfter time.Duration
	// Applier applies a sent draft's Effects; nil applies none.
	Applier Applier
	// Results attaches a sent post to its turn's task; nil attaches none.
	Results Results
}

func (*Service) ApplyPending added in v0.2.0

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

ApplyPending applies the effects of every sent draft not applied yet: a draft sent before a restart, or whose effects failed.

func (*Service) DeliveryOf added in v0.3.0

func (s *Service) DeliveryOf(ctx context.Context, g gate.Gate) (draft.State, error)

DeliveryOf is where the delivery of draft gate g's draft stands; "" for a gate of another kind.

func (*Service) Discard

func (s *Service) Discard(ctx context.Context, id draft.ID, reason string) (draft.Draft, error)

Discard drops queued or failed draft id, whose gate a person approved: a held draft's discard is its gate's.

func (*Service) Edit

func (s *Service) Edit(ctx context.Context, id draft.ID, text string) (draft.Draft, error)

Edit sets the text of held or failed draft id. A held draft waits for a person again: its gate is superseded first, so no window and no approval given to the old text releases the new one, and a gate that waits opens.

func (*Service) GateOf added in v0.3.0

func (s *Service) GateOf(ctx context.Context, d draft.Draft) (gate.Gate, bool, error)

GateOf is d's gate; false when it has none.

func (*Service) List

func (s *Service) List(ctx context.Context, f Filter) ([]draft.Draft, error)

List is the drafts f selects, each with the delivery its provider's publisher posts it as.

func (*Service) Outbox added in v0.3.0

func (s *Service) Outbox(ctx context.Context) ([]Pending, error)

Outbox are the drafts waiting on the user, newest first: those whose gate is open, and those whose gate approved them and whose delivery failed or a restart has yet to settle.

func (*Service) Prepare added in v0.3.0

func (s *Service) Prepare(ctx context.Context, id draft.ID, rev int64) (draft.Draft, error)

Prepare readies held draft id for a person to approve its gate: the draft at rev, which its publisher must take. It writes nothing. The caller reads the gate it resolves before it prepares: an edit after the person saw the draft supersedes that gate, and a person who changes the text approves through Edit's new gate.

func (*Service) Propose

func (s *Service) Propose(ctx context.Context, p Proposal) (draft.Draft, error)

Propose stores the draft p proposes as held and opens its gate under p's approval: a policy that releases it queues it at once, a dry_run drops it, a delay gives the gate its window.

func (*Service) Publish added in v0.3.0

func (s *Service) Publish(ctx context.Context, g gate.Gate) error

Publish is the consequence of draft gate g's resolution on its held draft: approved queues it for the sender, any other status drops it. A draft a rule or a window released fails instead, for a person to retry, when its publisher now posts it as another delivery than the one its approval decided, or when the workflow that proposed it no longer loads.

func (*Service) PublisherOf added in v0.3.0

func (s *Service) PublisherOf(d draft.Draft) unit.ProviderID

PublisherOf is the provider that posts d: its unit's when that one publishes, else the only provider of Targets whose publisher takes d's target, else its unit's.

func (*Service) RecordSent

func (s *Service) RecordSent(ctx context.Context, p Proposal, ev draft.Evidence) (draft.Draft, error)

RecordSent stores a post already made outside the outbox (origin session), by p.Tool, as sent, with a gate already approved by the agent that made it. It resolves no gate: no agent resolves an open one.

func (*Service) Recover

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

Recover runs once at daemon start: every sending draft -> Verify -> sent | failed, then the effects of every sent draft not applied yet; a draft whose provider answers ErrDisabled stays sending for the next Recover.

func (*Service) Retry

func (s *Service) Retry(ctx context.Context, id draft.ID, confirm bool) (draft.Draft, error)

Retry: failed -> queued. A draft that failed as "not confirmed after a restart" or with an Error starting "may have been posted" needs confirm true, else ErrNeedsConfirm.

func (*Service) Send

func (s *Service) Send(ctx context.Context) (sent, failed int, err error)

Send is the sender pass: the effects of the sent drafts not applied yet, then every approved draft -> sending -> sent | failed, one Publish each, no retry.

func (*Service) Withdraw added in v0.3.0

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

Withdraw drops draft id, which its proposer no longer stands by, for reason: a held draft's gate is superseded.

type Store

type Store interface {
	Insert(ctx context.Context, d draft.Draft) (draft.Draft, error) // assigns ID
	Get(ctx context.Context, id draft.ID) (draft.Draft, error)      // ErrNotFound
	List(ctx context.Context, f Filter) ([]draft.Draft, error)      // newest first
	// Update writes d only while the stored state is from and the stored Rev is d.Rev, and returns
	// d with Rev bumped; ErrConflict otherwise.
	Update(ctx context.Context, d draft.Draft, from draft.State) (draft.Draft, error)
}

Directories

Path Synopsis
Package outboxtest holds fakes of the Outbox's ports.
Package outboxtest holds fakes of the Outbox's ports.

Jump to

Keyboard shortcuts

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