workerstate

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package workerstate persists worker-side UI state that must survive a disconnect or process restart. It deliberately has no dependency on the TUI so recovery policy can be tested independently from rendering.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrIdentityUnavailable means authenticated gateway/worker identity has not
	// been bound yet. State must remain invisible until gateway Hello.
	ErrIdentityUnavailable = errors.New("worker state identity is not established")
	// ErrIdentityConflict prevents one Store instance from switching subjects
	// after it has exposed or mutated a namespace.
	ErrIdentityConflict = errors.New("worker state is bound to a different authenticated identity")
)

Functions

func RebindIdentity

func RebindIdentity(ctx context.Context, path, oldGatewayID, newGatewayID, workerID string) error

RebindIdentity validates and rewrites an offline staged state database for a restored gateway path. Every correctness row must belong to the declared old identity; a mixed archive is rejected rather than partially rewritten.

Types

type CorruptRecord

type CorruptRecord struct {
	Scope Scope
	Kind  string
	Err   error
}

CorruptRecord describes one row which List could not safely decode. Other healthy rows are still returned.

type CorruptRecordsError

type CorruptRecordsError struct {
	Records []CorruptRecord
}

CorruptRecordsError reports isolated row corruption. Callers may use the records returned alongside this error; none of the corrupt rows are included in that result.

func (*CorruptRecordsError) Error

func (err *CorruptRecordsError) Error() string

func (*CorruptRecordsError) Unwrap

func (err *CorruptRecordsError) Unwrap() error

type Identity

type Identity struct {
	GatewayID string `json:"gateway_id"`
	WorkerID  string `json:"worker_id"`
}

Identity is one authenticated gateway/worker namespace present in a state database. Tokens are deliberately excluded.

type Record

type Record struct {
	Scope     Scope           `json:"scope"`
	Kind      string          `json:"kind"`
	Payload   json.RawMessage `json:"payload"`
	UpdatedAt time.Time       `json:"updated_at"`
	// CreatedRevision is assigned once when the scope/kind row is inserted;
	// Revision changes on every Put. Both are database-local monotonic values and
	// are the only safe causal ordering source when wall clocks jump or tie.
	CreatedRevision int64 `json:"created_revision"`
	Revision        int64 `json:"revision"`
}

Record is one durable value. UpdatedAt is assigned by Store.Put and is always returned in UTC.

type Scope

type Scope struct {
	CallerID     string                    `json:"caller_id"`
	WorkspaceKey string                    `json:"workspace_key,omitempty"`
	TaskID       string                    `json:"task_id,omitempty"`
	SessionKey   string                    `json:"session_key"`
	Tier         completion.CapabilityTier `json:"tier"`
}

Scope is the correctness namespace for one piece of worker state. SessionKey is opaque: completion.Assignment.SessionKey contains a NUL separator and must not be treated as a path or a printable stable key.

type Store

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

Store is a single-process SQLite state store. SQLite access is serialized so concurrent Put/Delete/List calls share one transactional order.

func Open

func Open(ctx context.Context, path string) (*Store, error)

Open opens or creates a state database. A newly created parent directory is mode 0700 and the database is always mode 0600. Existing parent directories must already be private; Open never changes permissions on a caller-owned directory.

func (*Store) Bind

func (store *Store) Bind(ctx context.Context, gatewayID, workerID string) error

Bind selects the only authenticated correctness namespace this Store instance may access. It is idempotent for credential rotation because tokens are deliberately absent; a different gateway or worker subject is rejected.

func (*Store) Close

func (store *Store) Close() error

Close releases the database handle.

func (*Store) Delete

func (store *Store) Delete(ctx context.Context, scope Scope, kind string) error

Delete removes one exact scope/kind value. Deleting an absent value is successful, making cleanup idempotent across recovery attempts.

func (*Store) Identities

func (store *Store) Identities(ctx context.Context) ([]Identity, error)

Identities inspects namespace metadata without exposing any draft payload. It is intended for offline backup verification; ordinary TUI reads still require Bind and List.

func (*Store) List

func (store *Store) List(ctx context.Context) ([]Record, error)

List returns every healthy record in deterministic update/key order. Bad rows are isolated in CorruptRecordsError rather than hiding healthy state.

func (*Store) Put

func (store *Store) Put(
	ctx context.Context,
	scope Scope,
	kind string,
	payload json.RawMessage,
) (Record, error)

Put transactionally inserts or replaces a value in one scope. Payload is copied before Put returns, so later caller mutation cannot change the value represented by the returned Record.

Jump to

Keyboard shortcuts

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