Documentation
¶
Overview ¶
Package projects keeps the list of projects a person works in — added, created, cloned, or found from the sessions that ran there — with each one's live git state, its worktrees and what its sessions are doing (.ai/21-app.md § Projects, WP-05 and WP-08).
Git state is never polled. Each repository's `.git` (HEAD, index, refs, worktrees) is watched with fsnotify; a change is debounced and then answered by one `git status` under a timeout, with at most two git processes at once. An idle machine with fifty projects runs no git at all. The reference app ran about 29,000 background git commands in 17 days; this package exists partly so that number stays zero when nothing changes.
Index ¶
- Constants
- func CheckCloneURL(raw string) error
- type GitStatus
- type Op
- type Patch
- type Service
- func (s *Service) Add(ctx context.Context, path string) (View, bool, error)
- func (s *Service) AddWorktree(ctx context.Context, id int64, spec agents.WorktreeSpec) (agents.Worktree, error)
- func (s *Service) Clone(opID, rawURL, parent, name string) (Op, bool, error)
- func (s *Service) Close()
- func (s *Service) Create(ctx context.Context, parent, name string, gitInit bool) (View, error)
- func (s *Service) Get(ctx context.Context, id int64) (View, error)
- func (s *Service) GetOp(id string) (Op, bool)
- func (s *Service) GitRuns() int64
- func (s *Service) List(ctx context.Context) ([]View, error)
- func (s *Service) Ops() []Op
- func (s *Service) Project(id int64) (store.Project, bool)
- func (s *Service) ProjectFor(dir string) (store.Project, bool)
- func (s *Service) RemoveWorktree(ctx context.Context, id int64, name string) error
- func (s *Service) Start(ctx context.Context) error
- func (s *Service) Unlist(ctx context.Context, id int64) error
- func (s *Service) Update(ctx context.Context, id int64, patch Patch) (View, error)
- func (s *Service) Worktrees(ctx context.Context, id int64) ([]WorktreeView, error)
- type Sessions
- type View
- type WorktreeView
Constants ¶
const ( OpRunning = "running" OpDone = "done" OpFailed = "failed" )
Op states.
const ( // DefaultDebounce is how long a burst of changes to one `.git` is // gathered before git is asked once (21-app.md: 300 ms). DefaultDebounce = 300 * time.Millisecond // DefaultGitTimeout is how long one git command may take before it is // killed and the timeout reported. DefaultGitTimeout = 5 * time.Second )
Defaults for the service's knobs.
const ( FrameProject bus.FrameType = "project" FrameOp bus.FrameType = "op" )
FrameProject and FrameOp are the live frames this package sends: {"type":"project","data":View} and {"type":"op","data":Op}.
Variables ¶
This section is empty.
Functions ¶
func CheckCloneURL ¶
CheckCloneURL accepts an https:// URL or git's user@host:path ssh form — nothing else, so a clone can never name a local path, a file:// URL or a transport that runs a command (ext::).
Types ¶
type GitStatus ¶
type GitStatus struct {
Branch string `json:"branch"` // "" when detached
Detached bool `json:"detached,omitempty"` // HEAD is not on a branch
Dirty bool `json:"dirty"`
Changed int `json:"changed"` // files modified, staged, conflicted or untracked
Ahead int `json:"ahead"`
Behind int `json:"behind"`
Upstream string `json:"upstream,omitempty"`
DefaultBranch string `json:"default_branch,omitempty"`
RemoteURL string `json:"remote_url,omitempty"`
// Error is why the last refresh failed (git missing, a timeout, a broken
// repository), with git's own words; the other fields are then the last
// good answer, or zero.
Error string `json:"error,omitempty"`
// At is when git last answered, unix ms; 0 while the first answer is
// still on its way.
At int64 `json:"at"`
}
GitStatus is what git says about a project's working tree, as the sidebar shows it.
type Op ¶
type Op struct {
ID string `json:"op_id"`
Kind string `json:"kind"` // clone
State string `json:"state"` // running | done | failed
Phase string `json:"phase,omitempty"` // git's words: "Receiving objects", "Resolving deltas", …
Progress int `json:"progress"` // 0–100 within the phase
URL string `json:"url,omitempty"`
Dest string `json:"dest,omitempty"`
ProjectID int64 `json:"project_id,omitempty"` // set when done
Error string `json:"error,omitempty"`
StartedAt int64 `json:"started_at"`
UpdatedAt int64 `json:"updated_at"`
}
Op is a long operation — a clone — that outlives the request and the connection that asked for it. A client names it with an op_id of its own; asking again with the same id returns the same operation, so a phone that dropped mid-clone and retries does not start a second one. Its progress goes out as op frames on /v1/live, and GET /v1/projects/ops answers what a reconnecting client missed.
type Patch ¶
type Patch struct {
Name *string `json:"name,omitempty"`
Pinned *bool `json:"pinned,omitempty"`
Sort *int64 `json:"sort,omitempty"`
Defaults *json.RawMessage `json:"defaults,omitempty"`
}
Patch is what may be changed on a project.
type Service ¶
type Service struct {
Store *store.Store
Bus *bus.Bus
Log *slog.Logger
Now func() time.Time
// Eligible decides whether a repository a session ran in is listed
// without anyone adding it; store.ProjectWorthListing unless a test
// replaces it.
Eligible func(root string) bool
// Env is the environment a clone runs with (the login shell's, so ssh
// keys and credential helpers are found); os.Environ when nil.
Env func() []string
Debounce time.Duration
GitTimeout time.Duration
// contains filtered or unexported fields
}
Service is the projects list and its git watcher.
func (*Service) Add ¶
Add lists an existing folder. A folder inside a repository lists the repository, so its git state and its sessions are one row. Adding a folder already listed returns it; one that was unlisted is listed again.
func (*Service) AddWorktree ¶
func (s *Service) AddWorktree(ctx context.Context, id int64, spec agents.WorktreeSpec) (agents.Worktree, error)
AddWorktree creates a worktree on any branch and returns it.
func (*Service) Clone ¶
Clone starts cloning url into parent/<name> (name defaults to the repository's) and returns the operation at once. With an opID already known it returns that operation and starts nothing (existing is true).
func (*Service) Close ¶
func (s *Service) Close()
Close stops watching and asking git, and returns once no refresh is running. Safe to call more than once.
func (*Service) Create ¶
Create makes a new folder name under parent, runs `git init` in it when gitInit, and lists it. parent must exist; one level is made, never a chain.
func (*Service) ProjectFor ¶
ProjectFor is the listed project dir lies in (the longest root that holds it), or false.
func (*Service) RemoveWorktree ¶
RemoveWorktree removes a clean worktree Caprock made. Anything else is refused with the reason.
func (*Service) Start ¶
Start seeds the list on first run, starts watching every listed repository, asks git once about each, and follows the bus for sessions in folders not yet listed. It returns once the list is loaded; git answers arrive as project frames.
func (*Service) Unlist ¶
Unlist takes a project off the list. Nothing on disk is touched, and the sessions that ran there keep their cost and history.
type Sessions ¶
type Sessions struct {
Live int64 `json:"live"`
Waiting int64 `json:"waiting"`
Total int64 `json:"total"`
}
Sessions counts a project's sessions.
type View ¶
type View struct {
ID int64 `json:"id"`
Name string `json:"name"`
Root string `json:"root"` // the OS's own form of the path
Kind string `json:"kind"` // repo | folder
Source string `json:"source"`
Pinned bool `json:"pinned"`
Sort int64 `json:"sort"`
AddedAt int64 `json:"added_at"`
Archived bool `json:"archived,omitempty"`
Exists bool `json:"exists"` // the folder is on disk
Defaults json.RawMessage `json:"defaults"`
// Git is null for a folder that is not a repository.
Git *GitStatus `json:"git"`
Sessions Sessions `json:"sessions"`
CostToday float64 `json:"cost_today"`
LastActivity int64 `json:"last_activity"`
Worktrees []WorktreeView `json:"worktrees"`
// Removed is set on the frame sent when a project is taken off the list.
Removed bool `json:"removed,omitempty"`
}
View is a project as the API returns it.
type WorktreeView ¶
type WorktreeView struct {
Name string `json:"name"` // git's name for it (.git/worktrees/<name>)
Path string `json:"path"`
Branch string `json:"branch,omitempty"`
Head string `json:"head,omitempty"` // the commit, when detached
Caprock bool `json:"caprock"` // made by Caprock, so removable from here
Locked bool `json:"locked,omitempty"`
Missing bool `json:"missing,omitempty"` // its folder is gone (git calls it prunable)
Dirty bool `json:"dirty"`
Changed int `json:"changed"`
Error string `json:"error,omitempty"`
}
WorktreeView is one linked worktree of a project.