Documentation
¶
Overview ¶
Package journal makes every write hopsesh does undoable. Before a file is created, replaced, appended to or moved, the journal records how to reverse it (and keeps a copy of what is replaced), on disk, so an interrupted move can still be undone. Beyond files, it records a branch pushed to a git remote, a session started in a vendor's cloud and a session file a vendor's CLI wrote; their undo runs git or the module, through Reach.
Index ¶
- Constants
- Variables
- func Dir(stateDir string) string
- type Clouds
- type Entry
- type Git
- type Journal
- func (j *Journal) AddKey(k agent.SessionKey)
- func (j *Journal) AddPart(id string) error
- func (j *Journal) AddRemote(machine, id string) error
- func (j *Journal) Adopt(fsys host.FS, machine, p string) error
- func (j *Journal) Append(fsys host.FS, machine, p string, b []byte, o agent.AppendOptions) error
- func (j *Journal) Changed(ctx context.Context, r Reach) error
- func (j *Journal) ChangedBesides(ctx context.Context, r Reach, later []*Journal) error
- func (j *Journal) Cloud(machine string, s agent.CloudSession) error
- func (j *Journal) DeletedRef(machine, dir, remote, ref, sha string) error
- func (j *Journal) Forget(machine string) error
- func (j *Journal) KeepPushed(machine, dir, remote, ref, sha string) error
- func (j *Journal) PendingReceipts() bool
- func (j *Journal) Place(machine, staged, dst string, perm fs.FileMode) error
- func (j *Journal) PushRef(machine, dir, remote, ref, sha string) error
- func (j *Journal) RecordCreated(machine, p string) error
- func (j *Journal) RecoverReceipts(fsFor func(string) (host.FS, error)) error
- func (j *Journal) Ref(machine, dir, ref, sha, prev string) error
- func (j *Journal) Rename(fsys host.FS, machine, from, to string) error
- func (j *Journal) RenamedBranch(machine, dir, from, ref, sha string, created bool) error
- func (j *Journal) Save() error
- func (j *Journal) Seal(fsFor func(machine string) (host.FS, error)) error
- func (j *Journal) SetAside(machine, p string) error
- func (j *Journal) SetPartOf(id string) error
- func (j *Journal) Undo(ctx context.Context, r Reach, force bool) error
- func (j *Journal) UndoAfter(ctx context.Context, r Reach, force bool, later []*Journal) error
- func (j *Journal) Worktree(machine, dir, path string) error
- func (j *Journal) WriteFile(fsys host.FS, machine, p string, b []byte, perm fs.FileMode) error
- func (j *Journal) WriteReceipt(fsys host.FS, machine, path string, body []byte, guardNative bool) error
- type Manual
- type Op
- type Reach
- type Receipt
- type Refs
- type Remote
- type State
Constants ¶
const ( KindMove = "move" // a session brought here in its own agent KindContinue = "continue" // a session continued in another agent KindPush = "push" // a session sent to another machine (its lineage here) KindHandoff = "handoff" // a session handed off to a cloud (a branch, a cloud session, lineage) KindFetch = "fetch" // a session brought from a cloud (a worktree, an adopted session) KindHop = "hop" // a cloud session handed on to another cloud through this machine (Parts) KindCleanup = "cleanup" // branches deleted on a remote once their work was merged KindRename = "rename" // a session given a new title in its agent's own data )
Kinds of operations.
Variables ¶
var ErrChanged = errors.New("changed since")
ErrChanged means a file the operation wrote changed since: undoing it would lose that later work.
var ErrManual = errors.New("only the user can archive it, on its page")
ErrManual means a cloud cannot archive its sessions from hopsesh: the user does it on the session's page.
Functions ¶
Types ¶
type Clouds ¶ added in v0.4.0
type Clouds interface {
// Updated is when the session last changed, as the cloud lists it (zero when the
// cloud does not say).
Updated(ctx context.Context, cloud string, key agent.SessionKey) (time.Time, error)
// Archive archives the session, or returns ErrManual when the cloud cannot.
Archive(ctx context.Context, cloud string, key agent.SessionKey) error
}
Clouds reaches the sessions an operation started in vendor clouds, through the modules' cloud capabilities.
type Entry ¶
type Entry struct {
Op Op `json:"op"`
Machine string `json:"machine"`
Path string `json:"path"`
From string `json:"from,omitempty"` // OpRename
Size int64 `json:"size,omitempty"` // OpAppend: size before
Len int64 `json:"len,omitempty"` // OpAppend: bytes appended
Sum string `json:"sum,omitempty"` // OpAppend: their SHA-256
Mtime time.Time `json:"mtime,omitempty"` // OpAppend: time before
Backup string `json:"backup,omitempty"` // OpReplace: copy of the old file
KeepMtime bool `json:"keepMtime,omitempty"`
Created bool `json:"created,omitempty"` // OpAppend: the append created the file
// OpAppend: lines written later do not build on these bytes (AppendOptions.Standalone).
Standalone bool `json:"standalone,omitempty"`
// OpPushRef: Path is the checkout it was pushed from, Remote the remote's name there,
// Ref the full ref name and Sha the commit pushed.
Remote string `json:"remote,omitempty"`
Ref string `json:"ref,omitempty"`
Sha string `json:"sha,omitempty"`
Prev string `json:"prev,omitempty"` // OpRef: what the ref was before ("" for a new one)
// OpCloud: the cloud, the session (its key: the module and the vendor's id), its page,
// and when it last changed as hopsesh left it. Machine is the one that ran the driver.
Cloud string `json:"cloud,omitempty"`
Key *agent.SessionKey `json:"key,omitempty"`
URL string `json:"url,omitempty"`
Updated time.Time `json:"updated,omitempty"`
// OpAdopt uses Size and Sum for the whole file as adopted.
// Keep (OpPushRef): the user chose to keep the branch (delete_branch = "never"), so undo
// leaves it where it is and says so.
Keep bool `json:"keep,omitempty"`
}
Entry is one journaled write.
type Git ¶ added in v0.4.0
type Git interface {
// Ref returns the commit ref points at in the checkout dir ("" when there is no such
// ref, or no such checkout any more).
Ref(ctx context.Context, dir, ref string) (string, error)
// SetRef sets ref to sha ("" deletes it) if it still points at expect (a lease).
SetRef(ctx context.Context, dir, ref, sha, expect string) error
// RenameBranch renames a branch (full ref names).
RenameBranch(ctx context.Context, dir, from, to string) error
// WorktreeDirty reports whether a worktree has changes that are not committed (false
// when it is gone).
WorktreeDirty(ctx context.Context, worktree string) (bool, error)
// RemoveWorktree removes a worktree of the checkout dir (force: with its changes).
RemoveWorktree(ctx context.Context, dir, worktree string, force bool) error
}
Git reaches checkouts on this machine, for the worktrees and refs an operation made; the repos package implements it.
type Journal ¶
type Journal struct {
UndoTime time.Time `json:"undoTime,omitempty"`
Receipts []Receipt `json:"receipts,omitempty"`
TransferID string `json:"transferId,omitempty"`
ID string `json:"id"`
Kind string `json:"kind"` // what kind of operation (Kind*)
Title string `json:"title"`
Time time.Time `json:"time"`
Keys []agent.SessionKey `json:"keys"` // the sessions it created or changed
Entries []Entry `json:"entries"`
Undone bool `json:"undone,omitempty"`
// Remote are journals of the same operation kept by hopsesh on other machines (a push):
// undoing this one undoes them too.
Remote []Remote `json:"remote,omitempty"`
// After is the state the operation left each file it created or replaced in (Seal), so
// undo can tell when one was used afterwards and refuse to lose that work.
After []State `json:"after,omitempty"`
// Manual are steps undo could not take, which the user owes: a cloud session hopsesh
// cannot archive stays in the vendor's list until the user archives it there.
Manual []Manual `json:"manual,omitempty"`
// Kept are the branches undo left on their remotes because the user chose to keep them
// ("origin hopsesh/handoff/…").
Kept []string `json:"kept,omitempty"`
// Parts are the journals of the legs of a composite operation (a cloud-to-cloud hop:
// the fetch, then the hand-off), in order; undoing it undoes them, last first. PartOf
// is the composite operation a leg belongs to.
Parts []string `json:"parts,omitempty"`
PartOf string `json:"partOf,omitempty"`
// contains filtered or unexported fields
}
Journal is the undo record of one operation (a move, a continuation, a hand-off).
func (*Journal) AddKey ¶
func (j *Journal) AddKey(k agent.SessionKey)
AddKey records a session the journal concerns.
func (*Journal) AddPart ¶ added in v0.4.0
AddPart records the journal of a leg of this composite operation (in order).
func (*Journal) Adopt ¶
Adopt records a session file a vendor's CLI just wrote on machine for this operation (its state now), so undo sets it aside, unless it grew since.
func (*Journal) Append ¶
Append adds to a file, remembering where and what, so undo can take out exactly these bytes, even from between lines written later when they are Standalone.
func (*Journal) Changed ¶
Changed reports the first thing that changed since the operation (ErrChanged with what changed), or nil: a file it created or replaced, or one it appended to (not Standalone) that has more after its bytes; a file a vendor's CLI wrote that grew; a ref it pushed that moved on; a cloud session with activity since. What r cannot reach (a machine, a remote, a cloud) does not count, nor does a file or a ref that is gone.
func (*Journal) ChangedBesides ¶ added in v0.4.0
ChangedBesides is Changed for an operation later ones built on (the first leg of a hop): a file a later journal wrote too is left to that journal's own check, since undoing the later one first puts it back.
func (*Journal) Cloud ¶ added in v0.4.0
func (j *Journal) Cloud(machine string, s agent.CloudSession) error
Cloud records a session the operation started in a cloud (machine ran the driver).
func (*Journal) DeletedRef ¶ added in v0.4.0
DeletedRef records a branch about to be deleted on remote (it points at sha), so undo pushes it back.
func (*Journal) Forget ¶
Forget drops the entries for a machine whose own hopsesh journals those writes (the sender of a push replays and journals them there).
func (*Journal) KeepPushed ¶ added in v0.4.0
KeepPushed records a ref about to be pushed that undo must leave where it is: the user chose to keep it (it is reported in Kept).
func (*Journal) PendingReceipts ¶ added in v0.4.0
func (*Journal) Place ¶
Place installs a staged local file at dst on this machine (moving, not copying, when both are on one volume). An existing dst is kept in the journal.
func (*Journal) PushRef ¶ added in v0.4.0
PushRef records a ref about to be pushed from the checkout dir on machine to remote, so undo can delete it again (only while it still points at sha).
func (*Journal) RecordCreated ¶ added in v0.4.0
RecordCreated records a file another program just created for this operation (an agent's own importer), so undo removes it.
func (*Journal) RecoverReceipts ¶ added in v0.4.0
func (*Journal) Ref ¶ added in v0.4.0
Ref records a ref hopsesh set to sha in the checkout dir on machine (prev: what it was, "" when new), so undo puts it back.
func (*Journal) RenamedBranch ¶ added in v0.4.0
RenamedBranch records a branch renamed from from to ref (both full ref names) at sha in the checkout dir; created: the operation made the branch too, so undo deletes it rather than giving back the old name.
func (*Journal) Seal ¶
Seal records the state the operation left each file it created or replaced in. Files on machines fsFor cannot reach are left out (their undo is not checked).
func (*Journal) SetPartOf ¶ added in v0.4.0
SetPartOf records the composite operation this journal is a leg of.
func (*Journal) Undo ¶
Undo reverses a journal's entries, newest first, reaching machines, remotes and clouds through r (what it cannot reach is reported). Unless force, it refuses (ErrChanged) when something the operation did changed since: undoing would lose that later work. A forced undo deletes a pushed ref wherever it points now. A cloud session undo cannot archive is recorded in Manual, for the user.
func (*Journal) UndoAfter ¶ added in v0.4.0
UndoAfter permits only lineage compensation written by an already undone leg of the same composite transaction. Native transcript and code checks remain active.
func (*Journal) Worktree ¶ added in v0.4.0
Worktree records a worktree hopsesh added at path to the checkout dir on machine, so undo removes it.
type Manual ¶ added in v0.4.0
type Manual struct {
Cloud string `json:"cloud"`
Key agent.SessionKey `json:"key"`
URL string `json:"url,omitempty"`
}
Manual is a cloud session undo could not archive: the user archives it on its page.
type Op ¶
type Op string
Op is a journaled operation.
const ( OpCreate Op = "create" // undo: remove OpReplace Op = "replace" // undo: put the backup back OpAppend Op = "append" // undo: cut out the appended bytes (Size, Len, Sum) OpRename Op = "rename" // undo: move back // OpPushRef is a ref pushed to a git remote. Undo deletes it, with a lease: only while it // still points at what hopsesh pushed (Sha). OpPushRef Op = "push-ref" // OpCloud is a session started in a vendor's cloud. Undo archives it where the cloud // can; otherwise archiving it is a step the user owes (Journal.Manual). OpCloud Op = "cloud" // OpAdopt is a session file a vendor's CLI wrote for the operation (a teleported // transcript). Undo sets it aside in the journal, and refuses when it grew since. OpAdopt Op = "adopt" // OpWorktree is a worktree hopsesh added to a checkout on this machine (Path: the // worktree; From: the checkout). Undo removes it, and refuses while it holds changes. OpWorktree Op = "worktree" // OpRef is a ref hopsesh made or moved in a checkout on this machine (Path: the // checkout; Ref; Sha: what hopsesh set it to; Prev: what it was, "" for a new one). A // branch hopsesh renamed has From (its name before); Created says the operation made // that branch too. Undo puts it back while it still points at Sha. OpRef Op = "ref" // OpDeletedRef is a branch hopsesh deleted on a git remote once its work was merged (a // clean-up): Path is the checkout, Remote the remote's name there, Ref the full ref and // Sha the commit it pointed at. Undo pushes it back, and refuses when the remote has a // branch of that name again. OpDeletedRef Op = "deleted-ref" )
type Reach ¶ added in v0.4.0
type Reach struct {
// FS returns a machine's filesystem by name (an error when it cannot be reached; such
// entries are reported).
FS func(machine string) (host.FS, error)
// Refs reaches git remotes (nil: pushed refs are reported, not deleted).
Refs Refs
// Clouds reaches vendor clouds (nil: their sessions become steps the user owes).
Clouds Clouds
// Git reaches checkouts on this machine (nil: worktrees and refs are reported, not
// undone).
Git Git
}
Reach is how undo gets at what an operation changed.
type Receipt ¶ added in v0.4.0
type Receipt struct {
Machine string `json:"machine"`
Path string `json:"path"`
Body []byte `json:"body"`
Native *State `json:"native,omitempty"`
Applied bool `json:"applied"`
}
Receipt is a durable commit intention after native installation. Recovery replays sidecars only; it never writes the conversation again.
type Refs ¶ added in v0.4.0
type Refs interface {
// RemoteRef returns the commit ref points at on remote, as the checkout dir on machine
// sees it ("" when there is no such ref).
RemoteRef(ctx context.Context, machine, dir, remote, ref string) (string, error)
// DeleteRef deletes ref on remote if it still points at expect (a lease), and refuses
// otherwise.
DeleteRef(ctx context.Context, machine, dir, remote, ref, expect string) error
// RestoreRef pushes sha to ref on remote again, and refuses when the remote has a ref of
// that name.
RestoreRef(ctx context.Context, machine, dir, remote, ref, sha string) error
}
Refs reads and deletes refs on a checkout's git remote, for undo; the repos package implements it.