terminalwork

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package terminalwork implements the pure-domain durable terminal-work item state machine and store command shapes (requirements 8.1–8.9, design D9).

Work rows are independently idempotent actions with stable source keys, claim leases, and injectable-clock retry schedules. Domain code has no SQL, HTTP, or filesystem I/O. The application processor lives in the app subpackage; durable stores live under internal/infra/terminalwork/workstore.

Index

Constants

View Source
const MaxQueryLimit = 500

MaxQueryLimit is the hard upper bound for ListQuery.Limit.

Variables

View Source
var (
	ErrQueryTooBroad      = errors.New("terminalwork: query too broad")
	ErrQueryLimitExceeded = errors.New("terminalwork: query limit exceeded")
)

Functions

func HasSelectiveBound

func HasSelectiveBound(q ListQuery) bool

HasSelectiveBound reports whether q includes at least one selective filter.

func SameIntentReplay

func SameIntentReplay(a, b WorkRecord) bool

SameIntentReplay reports whether a and b describe the same durable intent payload for idempotent AppendIntent replay (requirements 8.5, design D6).

func ValidateListQuery

func ValidateListQuery(q ListQuery) error

ValidateListQuery rejects unsupported or too-broad operator filters.

Types

type AppendIntentOutcome

type AppendIntentOutcome struct {
	// Inserted is true only when this call definitively created the Intent row.
	Inserted bool
	// Replay is true only when an existing exact SameIntentReplay was confirmed.
	Replay bool
}

AppendIntentOutcome is the definitive result of a production AppendIntent attempt. Inserted and Replay are mutually exclusive; neither is set when the call returns an ambiguous transport/store error.

func (AppendIntentOutcome) Definitive

func (o AppendIntentOutcome) Definitive() bool

Definitive reports whether the outcome classifies the append unambiguously.

type BoundVersions

type BoundVersions struct {
	GenerationID        string // executable-policy generation (persisted generation_id)
	RuntimeInstanceID   string // opaque process/manager incarnation
	RuntimeGenerationID string // request-plane runtime configuration generation
	ProviderID          string
	RatingID            string
}

BoundVersions records generation/provider/rating versions bound at admit.

Identity rules (task 3.6):

  • GenerationID is the executable-policy / snapshotgen generation identity. The historical field name is retained for persisted rows, storage codecs, and SameIntentReplay determinism. Prefer ExecutableGenerationID().
  • RuntimeInstanceID is the opaque runtimehost.Manager process incarnation.
  • RuntimeGenerationID is the runtimehost request-plane generation identity (GenerationMeta.ID). Together with RuntimeInstanceID it forms durable runtime identity; numeric IDs alone are not restart-safe.
  • These identities must never be conflated with each other or with model registry/catalog generations.

func (BoundVersions) ExecutableGenerationID

func (v BoundVersions) ExecutableGenerationID() string

ExecutableGenerationID returns the executable-policy generation identity.

func (BoundVersions) HasRuntimeGeneration

func (v BoundVersions) HasRuntimeGeneration() bool

HasRuntimeGeneration reports whether an exact runtime configuration identity was bound (both instance and generation non-empty).

func (BoundVersions) RuntimeIdentity

func (v BoundVersions) RuntimeIdentity() RuntimeIdentityClass

RuntimeIdentity returns the runtime identity classification for this row.

type BoundedError

type BoundedError struct {
	Code      string
	Permanent bool
	Message   string
}

BoundedError is a content-safe work error classification.

type ClaimDueCommand

type ClaimDueCommand struct {
	OwnerID    string
	TTL        time.Duration
	Limit      int
	Now        time.Time
	ProviderID string
	Kind       sdk.WorkKind
}

ClaimDueCommand selects and claims a bounded batch of due work.

type ClaimLease

type ClaimLease struct {
	OwnerID   string
	ExpiresAt time.Time
}

ClaimLease coordinates workers, not users.

func (ClaimLease) HeldAt

func (l ClaimLease) HeldAt(now time.Time) bool

HeldAt reports whether the lease is still held at now.

type Clock

type Clock interface {
	Now() time.Time
}

Clock is an injectable time source for retry/lease decisions.

type CompleteCommand

type CompleteCommand struct {
	WorkID          string
	ExpectedOwnerID string
	Now             time.Time
}

CompleteCommand marks claimed work completed for the expected owner.

type LifecycleCorrelation

type LifecycleCorrelation struct {
	RequestID string
	AttemptID string
	TraceID   string
}

LifecycleCorrelation ties work to safe request/attempt identities.

type ListPage

type ListPage struct {
	Records []WorkRecord
	Cursor  string
}

ListPage is one bounded page of work records.

type ListQuery

type ListQuery struct {
	WorkID        string
	SourceKey     SourceKey
	State         sdk.WorkState
	States        []sdk.WorkState
	ProviderID    string
	Kind          sdk.WorkKind
	RequestID     string
	AttemptID     string
	DueBefore     time.Time
	UpdatedAfter  time.Time
	UpdatedBefore time.Time
	Limit         int
	Cursor        string
}

ListQuery is a bounded operator filter for terminal-work rows (requirement 8.9, 12.6–12.7).

type PromotePendingCommand

type PromotePendingCommand struct {
	WorkID string
	Now    time.Time
}

PromotePendingCommand moves intent rows to pending claim eligibility.

type QuarantineCommand

type QuarantineCommand struct {
	WorkID string
	Err    BoundedError
	Now    time.Time
}

QuarantineCommand permanently parks malformed or non-retryable work.

type RenewClaimCommand

type RenewClaimCommand struct {
	WorkID  string
	OwnerID string
	TTL     time.Duration
	Now     time.Time
}

RenewClaimCommand extends a held claim lease for the same owner.

type RetrySchedule

type RetrySchedule struct {
	Initial    time.Duration
	Multiplier int
	Max        time.Duration
}

RetrySchedule computes deterministic backoff without jitter in the domain.

func (RetrySchedule) Delay

func (s RetrySchedule) Delay(attempt int) time.Duration

Delay returns the backoff for the given 1-based attempt number.

type RuntimeIdentityClass

type RuntimeIdentityClass uint8

RuntimeIdentityClass classifies BoundVersions runtime identity material.

const (
	// RuntimeIdentityLegacy is both runtime instance and generation empty —
	// process-registry compatible rows from before task 3.6.
	RuntimeIdentityLegacy RuntimeIdentityClass = iota
	// RuntimeIdentityExact is both runtime instance and generation non-empty.
	RuntimeIdentityExact
	// RuntimeIdentityMalformed is only one of instance/generation populated.
	// It never falls back to legacy process-registry resolution.
	RuntimeIdentityMalformed
)

type ScheduleRetryCommand

type ScheduleRetryCommand struct {
	WorkID          string
	ExpectedOwnerID string
	Schedule        RetrySchedule
	Err             BoundedError
	Now             time.Time
}

ScheduleRetryCommand schedules a non-permanent retry for claimed work.

type SourceKey

type SourceKey struct {
	IdentityVersion int
	Key             string
}

SourceKey is a stable versioned work identity key (requirement 8.2).

func (SourceKey) String

func (k SourceKey) String() string

String returns the stable versioned key encoding.

func (SourceKey) Validate

func (k SourceKey) Validate() error

Validate rejects non-positive versions and empty/whitespace keys.

type WorkItem

type WorkItem struct {
	SourceKey   SourceKey
	WorkID      string
	Kind        sdk.WorkKind
	State       sdk.WorkState
	ProviderID  string
	Lifecycle   LifecycleCorrelation
	Versions    BoundVersions
	Attempts    int
	NextRetryAt time.Time
	Lease       ClaimLease
	Error       BoundedError
}

WorkItem is one independently idempotent terminal action.

func NewIntent

func NewIntent(source SourceKey, workID string, kind sdk.WorkKind, providerID string, life LifecycleCorrelation, ver BoundVersions) (*WorkItem, error)

NewIntent records durable intent first (design D9).

func (*WorkItem) Claim

func (w *WorkItem) Claim(ownerID string, ttl time.Duration, clock Clock) error

Claim attempts to claim due work with a lease.

func (*WorkItem) Complete

func (w *WorkItem) Complete() error

Complete marks the work completed.

func (*WorkItem) MarkPending

func (w *WorkItem) MarkPending() error

MarkPending promotes intent to pending.

func (*WorkItem) Quarantine

func (w *WorkItem) Quarantine(err BoundedError) error

Quarantine permanently parks malformed or non-retryable work.

func (*WorkItem) RenewClaim

func (w *WorkItem) RenewClaim(ownerID string, ttl time.Duration, clock Clock) error

RenewClaim extends a held claim lease for the same owner.

func (*WorkItem) Retry

func (w *WorkItem) Retry(schedule RetrySchedule, clock Clock, err BoundedError) error

Retry schedules a retry using the clock and schedule.

type WorkRecord

type WorkRecord struct {
	WorkID         string
	SourceKey      SourceKey
	PayloadVersion int
	Kind           sdk.WorkKind
	State          sdk.WorkState
	ProviderID     string
	Lifecycle      LifecycleCorrelation
	Versions       BoundVersions
	Payload        []byte
	FactID         string
	LeaseSetID     string
	Attempts       int
	NextRetryAt    time.Time
	Lease          ClaimLease
	Error          BoundedError
	CreatedAt      time.Time
	UpdatedAt      time.Time
}

WorkRecord is the persisted terminal-work row (requirements 8.1–8.2, design D14).

func (*WorkRecord) ApplyWorkItem

func (r *WorkRecord) ApplyWorkItem(w *WorkItem, now time.Time)

ApplyWorkItem copies domain transition results back into the record.

func (WorkRecord) ToWorkItem

func (r WorkRecord) ToWorkItem() *WorkItem

ToWorkItem projects persisted state into the domain item for transitions.

func (WorkRecord) Validate

func (r WorkRecord) Validate() error

Validate rejects malformed persisted records before store I/O.

Directories

Path Synopsis
Package app implements the bounded terminal-work processor and provider router (requirements 8.4–8.8, design D9).
Package app implements the bounded terminal-work processor and provider router (requirements 8.4–8.8, design D9).

Jump to

Keyboard shortcuts

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