mutation

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT 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 mutation is the guard every command that changes state goes through: the advisory lock that serializes authama processes, the crash-residue sweep, and the ~/.ssh snapshot that makes the change reversible.

It moved here from the command layer so that something other than the CLI - the web server - can guard its changes the same way.

Index

Constants

This section is empty.

Variables

View Source
var (

	// Disabled turns recording off. The tests that are not about history set
	// it; $AUTHAMA_HISTORY=0 does too (see off), read at use rather than at
	// start-up so a value from .env or authama.json applies.
	Disabled = false
)
View Source
var ErrBusy = errors.New("another change is being made from this authama serve; wait for it to finish, then try again")

ErrBusy reports that another change is being made by this process: in a server, a preview, a confirmed change or a job.

View Source
var ErrDeclined = errors.New("declined")

ErrDeclined marks an error that means the person, or the state, said no: a scope that ends with it is recorded as aborted, not as a failure.

Functions

func Begin

func Begin(s Scope)

Begin starts a scope. Scopes nest: the TUI is one scope, and every action chosen from its menu is its own scope inside it, recorded separately.

func End

func End(o Outcome)

End closes the innermost scope and, if it mutated, records how it ended and what the manifest and inventory were afterwards.

func Exclusive

func Exclusive(wait time.Duration) (release func(), err error)

Exclusive waits up to wait for the process's one change slot, and returns the function that gives it back. A job can hold the slot for as long as it waits on a person, so callers wait a short while and then say why.

func Held

func Held() bool

Held reports whether this process holds the advisory lock.

func InstallSink

func InstallSink()

InstallSink routes audit events into the history. The command layer calls it once at start-up; util/log cannot import the store itself.

func LockForMutation

func LockForMutation(p paths.Paths)

LockForMutation takes the advisory lock, once per process. A mutating command calls it before it reads the manifest or inventory, not just before it writes: one that loaded state first and then waited for the lock saved that stale copy afterwards, silently dropping whatever the command it waited for had written. SnapshotBeforeMutation calls it too, so a command that forgets still holds the lock while it mutates.

func Release

func Release()

Release gives up the lock this process holds, if any. A CLI command never needs to - exiting releases it - but a test that runs many commands in one process does, and so does a long-lived server between operations.

func Scoped

func Scoped(p paths.Paths, s Scope, fn func() error) error

Scoped runs fn as one operation under the lock and releases the lock when fn returns. It is for a long-lived process: a CLI command holds the lock until it exits, which a server cannot do without blocking every other authama command for as long as it runs. fn must not call Scoped itself.

func SnapshotBeforeMutation

func SnapshotBeforeMutation(p paths.Paths, retain int) string

SnapshotBeforeMutation is the native mutation guard (mirrors the Facade's _mutating): take the advisory lock so concurrent commands serialize, sweep crash residue, then snapshot ~/.ssh so the change is reversible. The lock is best- effort - a failure to acquire doesn't block the operation. Returns the snapshot path ("" if none was made).

The lock is taken once per process, not once per call. flock is per open file descriptor, and Acquire blocks until it is free, so a second call in the same process waits forever on a lock that same process is holding - a deadlock with no output at all. One CLI command mutates once and exits, which is why this went unnoticed; the TUI is one process that mutates repeatedly (reconcile, then pin, then rotate) and hangs on the second action. Nothing is given up by holding it: the lock exists to serialize separate processes, and within one process these run in sequence anyway.

func SourcesFor

func SourcesFor(p paths.Paths) history.Sources

SourcesFor names what a backfill reads for the config home p.

func Suspend

func Suspend() func()

Suspend stops recording until the returned function is called. A preview runs a change against a throwaway copy of the home, and what it does there is not something that happened. The caller must keep anything real from running meanwhile; change does, by previewing and applying one at a time.

Types

type Outcome

type Outcome struct {
	Err     error
	Aborted bool // the person declined a confirmation
}

Outcome is how a scope ended.

type Scope

type Scope struct {
	Command  string   // "host add"
	Args     []string // already redacted
	Source   string   // history.SourceCLI, SourceTUI, SourceWeb
	DryRun   bool
	Subjects []history.Subject
	OpID     string // optional: the id to record under, so a caller can link to it
}

Scope is one command a person asked for. It becomes an operation in the history only if it mutates: the guard opens the operation the first time the command takes the lock, so a read-only command records nothing, and a mutating one is recorded without code of its own.

Jump to

Keyboard shortcuts

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