engine

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 25 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 engine plans and performs a session transport: it decides where a session goes on this machine, which files move, how paths map, what to do about the repository and worktrees, and then stages, rewrites, verifies and commits the copy with an undo record.

Index

Constants

View Source
const (
	MarkNow         = "now"          // right after the move
	MarkWhenStopped = "when-stopped" // the old session is still running: on a later scan
	MarkOff         = "off"
)

Marking states in a plan.

Variables

View Source
var ErrBlocked = errors.New("plan has unresolved blockers")

ErrBlocked means the plan has unresolved blockers.

Functions

func Human

func Human(n int64) string

Human formats a byte count for display.

func LocalOS

func LocalOS() string

LocalOS is this machine's GOOS.

Types

type Env

type Env struct {
	StateDir string // staging/ and undo/ live here
	Log      *audit.Log
	// Progress, if set, receives one line per step.
	Progress func(step string)
}

Env is where Apply keeps its working state.

type FileItem

type FileItem struct {
	Src     string `json:"src"`  // path on the source filesystem
	Rel     string `json:"rel"`  // path relative to the target project dir
	Kind    string `json:"kind"` // transcript | subagent | tool-result | sidecar | file-history | memory
	Size    int64  `json:"size"`
	Rewrite string `json:"rewrite"` // jsonl | text | none
}

FileItem is one file to copy.

type Input

type Input struct {
	Summary *sessions.Summary
	Git     *repos.GitState // source git state of the session's directory (nil if unknown)
	Live    *sessions.LiveEntry
}

Input bundles what BuildPlan needs about the session.

type Moved

type Moved struct {
	From string `json:"from"`
	To   string `json:"to"`
}

Moved records a file moved out of the way.

type Options

type Options struct {
	TargetDir    string // explicit local directory to resume in (skips repo matching)
	ReposDir     string
	GHQLayout    bool
	Clone        bool // clone the repository if it is not found locally
	Worktree     WorktreeMode
	Fork         bool // keep the source session running (fork) instead of handing off
	RemoteCtl    bool
	NotifyOld    bool
	Redact       bool
	DropThinking bool // the target uses a different Anthropic account
	CopyMemory   bool // merge the project's auto-memory folder
	ExtraRoots   []string
	// Round trips.
	MarkSource bool // after a handoff, title the copy left behind "↪ moved to <this host> · <title>"
	SyncCode   bool // fetch the session's commit here and fast-forward a clean checkout to it
	PushSource bool // first push the source branch's unpushed commits from the source machine
	StopLocal  bool // quit a copy of this session that is running on this machine
	// Conflict says what to do when the copy here changed too: "" refuses, "replace"
	// sets it aside (undo keeps it), "keep-both" brings this copy in as a separate session.
	Conflict string
}

Options are the user's choices for one transport.

type Plan

type Plan struct {
	SessionID     string            `json:"sessionId"`
	Title         string            `json:"title"`
	SourceHost    string            `json:"sourceHost"`
	SourceOS      string            `json:"sourceOs"`
	SourceFile    string            `json:"sourceFile"`
	SourceCWD     string            `json:"sourceCwd"`
	SourceVersion string            `json:"sourceVersion,omitempty"`
	Live          bool              `json:"live"`
	OldName       string            `json:"oldName,omitempty"`
	Repo          RepoPlan          `json:"repo"`
	TargetCWD     string            `json:"targetCwd"`
	TargetDir     string            `json:"targetProjectDir"`
	TargetFile    string            `json:"targetFile"`
	Mappings      []rewrite.Mapping `json:"mappings"`
	Files         []FileItem        `json:"files"`
	TotalBytes    int64             `json:"totalBytes"`
	Duplicates    []string          `json:"duplicates,omitempty"` // existing copies to set aside
	Warnings      []string          `json:"warnings,omitempty"`
	Blockers      []string          `json:"blockers,omitempty"` // must be resolved before Apply
	Options       Options           `json:"options"`
	NewName       string            `json:"newName"`
	Resume        link.Resume       `json:"resume"`
	StartContext  link.Context      `json:"-"`
	// Round trips.
	Mark    string `json:"mark"`              // what happens to the copy left behind: now | when-stopped | off
	StopPID int    `json:"stopPid,omitempty"` // a local process of this session to quit first
	Push    bool   `json:"push,omitempty"`    // push the source branch first
	Sync    string `json:"sync,omitempty"`    // what code sync will do, in words ("" when not applicable)
	// When the copy here changed as well: what changed, and the original id when this
	// copy comes in under a new id (keep both).
	Conflict   string `json:"conflict,omitempty"`
	OriginalID string `json:"originalId,omitempty"`
	// SyncFromSource: unpushed commits will be fetched straight from the source machine.
	SyncFromSource bool `json:"syncFromSource,omitempty"`
}

Plan is everything a transport will do. Front-ends render it before Apply.

func BuildPlan

func BuildPlan(ctx context.Context, src Source, tgt Target, in Input, opt Options) (*Plan, error)

BuildPlan works out a transport without changing anything.

type RepoPlan

type RepoPlan struct {
	Identity     string   `json:"identity,omitempty"`
	Remote       string   `json:"remote,omitempty"`
	SourceTop    string   `json:"sourceTop,omitempty"`
	SourceMain   string   `json:"sourceMain,omitempty"`
	SourceBranch string   `json:"sourceBranch,omitempty"`
	MainBranch   string   `json:"sourceMainBranch,omitempty"` // branch checked out in the source's main folder
	InWorktree   bool     `json:"sourceInWorktree,omitempty"`
	ClaudeWT     bool     `json:"sourceClaudeWorktree,omitempty"`
	Action       string   `json:"action"` // use | clone | needs-clone | dir | none
	LocalPath    string   `json:"localPath,omitempty"`
	Alternatives []string `json:"alternatives,omitempty"`
	LocalBranch  string   `json:"localBranch,omitempty"` // branch currently checked out locally
	Worktree     string   `json:"worktree,omitempty"`    // worktree to create
	Unpushed     int      `json:"unpushed"`
	Dirty        int      `json:"dirty"`
	SourceHead   string   `json:"sourceHead,omitempty"`     // commit the session's checkout was at
	HasUpstream  bool     `json:"sourceUpstream,omitempty"` // the source branch tracks a remote branch
}

RepoPlan is what happens to the repository on this machine.

type Result

type Result struct {
	TargetFile string        `json:"targetFile"`
	UndoID     string        `json:"undoId"`
	Rewrite    rewrite.Stats `json:"rewrite"`
	Secrets    scan.Summary  `json:"secrets"`
	Copied     int           `json:"filesCopied"`
	Bytes      int64         `json:"bytesCopied"`
	Cloned     bool          `json:"cloned"`
	Worktree   string        `json:"worktreeCreated,omitempty"`
	SetAside   []string      `json:"setAside,omitempty"`
	// Round trips.
	Stopped   int               `json:"stoppedPid,omitempty"`
	Pushed    string            `json:"pushed,omitempty"`    // git's message after pushing on the source
	PushError string            `json:"pushError,omitempty"` // pushing failed; the move went on
	Sync      *repos.SyncResult `json:"sync,omitempty"`
	SyncNote  string            `json:"syncNote,omitempty"`
	Mark      string            `json:"mark"` // done | pending | off | failed
	MarkError string            `json:"markError,omitempty"`
}

Result reports a completed transport.

func Apply

func Apply(ctx context.Context, p *Plan, src Source, env Env) (*Result, error)

Apply performs the plan. On failure nothing is committed into Claude's folders.

type Source

type Source struct {
	Host      string
	OS        string // darwin | linux | windows
	FS        fsys.FS
	ConfigDir string
	Home      string
	Auth      *link.Auth // the login there, when it could be read
	// Push pushes the current branch of a directory there to its upstream (nil when the
	// machine cannot run commands, e.g. a plain file import). It returns git's message.
	Push func(ctx context.Context, dir string) (string, error)
	// GitFetch describes how to fetch from a repository there over SSH (nil when not
	// possible), so commits that were never pushed can still come along.
	GitFetch func(repoDir string) *repos.FetchSource
}

Source is the machine a session is copied from (possibly this machine).

type Target

type Target struct {
	Host      string
	OS        string
	ConfigDir string
	Home      string
	// ClaudeVersion is `claude --version` here, if known ("" = not installed or unknown).
	ClaudeVersion string
	ClaudePath    string     // the claude binary here, if found
	Auth          *link.Auth // the login here, when it could be read
}

Target is this machine.

type UndoManifest

type UndoManifest struct {
	ID         string    `json:"id"`
	SessionID  string    `json:"sessionId"`
	SourceHost string    `json:"sourceHost"`
	Time       time.Time `json:"time"`
	TargetFile string    `json:"targetFile"`
	Created    []string  `json:"created"`
	SetAside   []Moved   `json:"setAside"`
	Undone     bool      `json:"undone"`
}

UndoManifest is the journal of one transport.

func ListUndo

func ListUndo(stateDir string) ([]UndoManifest, error)

ListUndo returns transports that can be undone, newest first.

func Undo

func Undo(stateDir, match string, log *audit.Log) (*UndoManifest, error)

Undo reverses the newest transport of a session (match by session id prefix or undo id). Files created by the transport are removed and anything set aside is restored. Clones and worktrees are left in place (they may hold your work); the result says so.

type WorktreeMode

type WorktreeMode string

WorktreeMode says how to handle a session that ran in a git worktree (or on a branch the local checkout is not on).

const (
	WorktreeAuto   WorktreeMode = "auto"   // recreate the worktree if the session used one
	WorktreeCreate WorktreeMode = "create" // always use a worktree on the session's branch
	WorktreeMain   WorktreeMode = "main"   // use the main checkout as it is
)

Jump to

Keyboard shortcuts

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