Documentation
¶
Overview ¶
Package spawn opens new lich sessions for a caller that is not the window: the `lich` command line and the MCP tools beside it (internal/cli). An agent could already talk to the sessions around it (internal/relay); this is what lets it create one — with a git worktree under it when the work needs its own checkout.
It exists as its own service because opening a session is four things at once and no single package owns them: a worktree (internal/project), a row (internal/store), a PTY (internal/terminal) and a card in the window (internal/events). The window does the same four in its own order (native/cmd/lich-native/main.go, newSession).
The PTY is started here rather than left to the window, and that is the whole point of the feature. The window spawns a session's terminal only once somebody has looked at the card, so a session opened for an agent and never clicked would have no process in it — invisible to `lich sessions`, unreachable by `lich send`, useless to whoever asked for it. Starting it here makes it addressable the moment Open returns; the card's first view attaches to the PTY that is already running, exactly as it does when the window opens again on a backend that kept it.
Index ¶
- Constants
- type Chat
- type Checkout
- type CloseOptions
- type CloseProjectOptions
- type Closed
- type ClosedEvent
- type ClosedProject
- type ColorFolderOptions
- type Colored
- type ColoredEvent
- type ControlOptions
- type Controlled
- type Events
- type FileOptions
- type Filed
- type FiledEvent
- type Folder
- type FoldersOptions
- type HandoffOptions
- type OpenOptions
- type OpenSubagentOptions
- type Refiled
- type RenameFolderOptions
- type RenameOptions
- type Renamed
- type RenamedEvent
- type Service
- func (s *Service) Close(ctx context.Context, opts CloseOptions) (Closed, error)
- func (s *Service) CloseFinishedWorker(id string) error
- func (s *Service) CloseProject(ctx context.Context, opts CloseProjectOptions) (ClosedProject, error)
- func (s *Service) ColorFolder(_ context.Context, opts ColorFolderOptions) (Colored, error)
- func (s *Service) Control(ctx context.Context, opts ControlOptions) (Controlled, error)
- func (s *Service) File(ctx context.Context, opts FileOptions) (Filed, error)
- func (s *Service) Folders(ctx context.Context, opts FoldersOptions) ([]Folder, error)
- func (s *Service) Handoff(ctx context.Context, opts HandoffOptions) (Session, error)
- func (s *Service) Open(ctx context.Context, opts OpenOptions) (Session, error)
- func (s *Service) OpenSubagent(ctx context.Context, opts OpenSubagentOptions) (Session, error)
- func (s *Service) OpenSubagentNow(ctx context.Context, opts OpenSubagentOptions) (Session, error)
- func (s *Service) Project(ctx context.Context, opts api.ProjectRefOptions) (api.ProjectRef, error)
- func (s *Service) Rename(ctx context.Context, opts RenameOptions) (Renamed, error)
- func (s *Service) RenameFolder(ctx context.Context, opts RenameFolderOptions) (Refiled, error)
- func (s *Service) SetChat(c Chat)
- func (s *Service) Worktrees(ctx context.Context, opts WorktreesOptions) ([]Checkout, error)
- type Session
- type Sessions
- type Terminal
- type Worktrees
- type WorktreesOptions
Constants ¶
const ( // KeepWorktree leaves the checkout on disk and parks the session's row, so // its conversation can be resumed later — the window's "keep" answer. KeepWorktree = "keep" // RemoveWorktree deletes the checkout and the session with it. RemoveWorktree = "remove" )
What a caller may do with the checkout a session was the last one in. The window asks the same question with a dialog; a caller outside it has nobody to ask, so it has to say up front.
const ( ControlPrompt = "prompt" ControlAbort = "abort" ControlModel = "model" ControlEffort = "effort" ControlCommand = "command" )
The actions Control takes.
const ( ControlDone = "done" ControlDelivered = "delivered" ControlEnded = "ended" )
How far a command Control handed a session got by the time it answered.
const ControlWaitLimit = 60 * time.Second
ControlWaitLimit is the longest Control takes: a model a chat takes only by restarting resumes its conversation first. It stays under the 90 seconds an MCP call may block (cli.mcpMaxWait).
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chat ¶
type Chat interface {
// Drawn is whether session id is drawn as a chat.
Drawn(id string) bool
// Open starts session id's chat and returns once its conversation is
// open, so a task sent right after is queued, not lost.
Open(ctx context.Context, id string) error
// Take sends text as session id's next message, after the turn running
// if one is, and waits on that turn's start.
Take(id, text string) (relay.HandedPrompt, error)
// Cancel stops the turn session id's chat is running.
Cancel(ctx context.Context, opts api.SessionIDOptions) (api.None, error)
// Options lists what session id's chat can be set to.
Options(ctx context.Context, opts api.SessionIDOptions) ([]api.ChatOption, error)
// SetOption sets one of them, the way the chat's picker does.
SetOption(ctx context.Context, opts api.ChatSetOptionOptions) (api.None, error)
}
Chat is the chat service (internal/chat) as a spawn needs it: a session the store drew as a chat at its birth has no terminal to start.
type Checkout ¶
type Checkout = api.WorktreeCheckout
type CloseOptions ¶
type CloseOptions = api.CloseSessionOptions
type CloseProjectOptions ¶ added in v0.81.0
type CloseProjectOptions = api.CloseProjectOptions
type Closed ¶
type Closed = api.ClosedSession
type ClosedEvent ¶
type ClosedEvent = api.SessionClosed
type ClosedProject ¶ added in v0.81.0
type ClosedProject = api.ClosedProject
type ColorFolderOptions ¶
type ColorFolderOptions = api.ColorFolderOptions
type Colored ¶
type Colored = api.ColoredFolder
type ColoredEvent ¶
type ColoredEvent = api.SessionsColored
type ControlOptions ¶
type ControlOptions = api.ControlSessionOptions
type Controlled ¶
type Controlled = api.ControlledSession
type Events ¶
Events is where an opened session is announced to the window. A nil one leaves the feature working and silent — the session runs, its card appears on the next reload.
type FileOptions ¶
type FileOptions = api.FileSessionOptions
type Filed ¶
type Filed = api.FiledSession
type FiledEvent ¶
type FiledEvent = api.SessionsFiled
type Folder ¶
type Folder = api.SessionFolder
type FoldersOptions ¶
type FoldersOptions = api.FoldersOptions
type HandoffOptions ¶ added in v0.87.0
type HandoffOptions = api.HandoffSessionOptions
type OpenOptions ¶
type OpenOptions = api.OpenSessionOptions
type OpenSubagentOptions ¶
type OpenSubagentOptions = api.OpenSubagentOptions
type Refiled ¶
type Refiled = api.RenamedFolder
type RenameFolderOptions ¶
type RenameFolderOptions = api.RenameFolderOptions
type RenameOptions ¶
type RenameOptions = api.RenameSessionOptions
type Renamed ¶
type Renamed = api.RenamedSession
type RenamedEvent ¶
type RenamedEvent = api.SessionTitle
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service opens sessions on behalf of a caller outside the window.
func (*Service) Close ¶
Close closes a session, and settles what happens to the checkout it was the last one in.
The window asks about that in a dialog, twice over when the checkout has uncommitted work. Nobody can be asked here, so the answer is an argument: closing the last session of a worktree without one is an error naming the choice rather than a guess. Removing a checkout that is dirty needs force on top, because what it discards exists in no other place — not in a commit, not on a remote, not in the session that was working on it.
A session that shares its checkout with another, or lives in the project's own directory, has nothing at stake and closes on the spot.
func (*Service) CloseFinishedWorker ¶
CloseFinishedWorker closes a subagent worker (OpenSubagent) that has reported back and ended its turn, when it runs in its caller's checkout: like a native subagent that returned, it has nothing of its own left to keep. The close is the parking Close performs, so its conversation stays resumable from the history.
A worker on a worktree of its own is left open, since that checkout holds work nothing else does, and so is one the user pinned and one whose caller is no longer in the workspace, which leaves no checkout to call shared. A worker already gone is not an error. The relay calls this (relay.SetWorkerFinished).
func (*Service) CloseProject ¶ added in v0.81.0
func (s *Service) CloseProject(ctx context.Context, opts CloseProjectOptions) (ClosedProject, error)
CloseProject takes a project off the window's strip the way its tab's close does: the row is marked closed with its sessions kept, so a reopen (an open on its path, or the window's history) brings them back, and their terminals are stopped. Nothing about a checkout is decided, because nothing is removed.
The window stops the terminals itself after its own close (native/cmd/lich-native/topbar_apply.go, closeProject) and only drops its views on project-closed, so a close from here stops them in the backend, in the window's order: the row first, so a refused close leaves every session running.
Two projects are refused. The caller's own, because closing it takes down the agent asking, halfway through the call. And Home, which the window keeps open for good (errHomeIsPermanent there).
func (*Service) ColorFolder ¶
ColorFolder paints every session filed under one of a project's folders, the window's folder "Color" from outside it; an empty colour hands them back to the theme. Project names it; empty takes the caller's own.
The folder is matched exactly and refused when no session carries it, for the reason RenameFolder gives. The colour is one of the window's palette names, in any case; a name outside it is refused, since the window would draw it as no colour and the caller would be told otherwise.
func (*Service) Control ¶
func (s *Service) Control(ctx context.Context, opts ControlOptions) (Controlled, error)
Control gives a session drawn as a chat one command, the way its person would in the chat: a prompt or a slash command as its next message, an abort as Stop, a model or an effort through its picker. A prompt waits a bounded time for its turn to start; one still queued behind a running turn is not an error, and State says so. A terminal session is refused on every provider: lich has no channel into a TUI but typing.
func (*Service) File ¶
File files a session under a folder, the window's "Move to folder" from outside it; an empty folder takes the session out of the one it is in. A name no session carries yet is a folder that starts existing with this one in it, as it is in the window.
target is the session to file, by either of the names it answers to; empty files the caller's own, as a rename does.
func (*Service) Folders ¶
Folders lists a project's folders. Project names it; empty takes the caller's own, exactly as Worktrees does.
func (*Service) Handoff ¶ added in v0.87.0
Handoff opens the successor of the session From runs in: a fresh session in the caller's checkout and folder, on the caller's provider, model and effort unless opts names others, with the caller's ultracode and profile on that provider, so an agent at a clean break can pass its work on instead of carrying a degraded context. The caller is left open; closing it is the user's call. A profile named in opts is only put to the user: it changes nothing here, and one that cannot apply is reported, not a failed handoff.
func (*Service) OpenSubagent ¶
OpenSubagent opens a session as the subagent of the session From runs in (`lich open --subagent`), the way lich-plugin's hook turns a provider's own subagent call into a card (docs/hooks/subagent-cards.md). It is Open with three differences, each of which makes the card behave like the subagent it replaces:
- without a worktree it opens in the caller's checkout — its worktree, or the project directory it runs in — rather than the project's own directory, so the worker edits what the caller sees;
- it is filed with the caller, so workers sit under whoever asked for them: in the caller's folder, or one named after the caller's label that the caller is filed under too (subagentFolder);
- its row records how deep it runs, one level below the caller (SetSessionSubagentDepth), so every spawn of it knows whether its own subagents may still become cards (internal/terminal, subagentCardsOn).
The project is always the caller's. A caller that is not a session lich holds is refused before anything is opened: there is no checkout to share and no one to report to.
func (*Service) OpenSubagentNow ¶
OpenSubagentNow is OpenSubagent answering once the session exists, its row and label, and starting its agent in the background: /subagent answers in the time a hook waits, and a chat's handshake can outlast it. A start that fails is logged and shows on the card; the task waiting for its prompt ends undelivered, as any errand to a session that never comes up.
func (*Service) Project ¶ added in v0.84.0
func (s *Service) Project(ctx context.Context, opts api.ProjectRefOptions) (api.ProjectRef, error)
Project resolves an open project as every other spawn call does, for a caller that goes on to a method keyed by project id.
func (*Service) Rename ¶
Rename gives a session the name on its card, the window's rename from outside the window. Like the window's, it makes the name the user's: the provider's ai-title never stomps a chosen name again (store.RenameSession).
Target is the session to rename, by either of the names it answers to; empty renames the caller's own, which is the one form an agent can reach without discovery — list_sessions shows it every session but itself.
A name another session in that project already holds is refused rather than written, because two sessions under one label is the one thing `lich send` cannot resolve. The window has no such rule: it renames what the user is pointing at, and the user can see which card they meant.
func (*Service) RenameFolder ¶
RenameFolder renames one of a project's folders, or takes it apart when To is empty: the window's "Rename folder" and "Ungroup" from outside it. Project names it; empty takes the caller's own.
The name is matched exactly, as the store matches it. One no session carries is refused rather than written: the store would match no row and answer success, and a caller told its rename landed would go on to address a folder that is not there. Renaming onto a name the project already holds merges the two, as it does in the window (docs/ceilings.md), which is why the answer names every session that moved. What moved is what the store's write reports, not this call's own read: a card filed under the old name in between is rewritten too. Parked sessions move with the rest but have no card, so the answer names the open ones.
func (*Service) SetChat ¶
SetChat wires the chat service. Without it every session starts its terminal, which is what every test that does not wire it gets.
func (*Service) Worktrees ¶
Worktrees lists a project's checkouts. Project names it; empty takes the caller's own, exactly as Open does.
The project's own directory is not one of them: it is the checkout every project has, it cannot be removed, and offering it in a list whose point is "what can be opened or closed" would invite exactly that.
type Session ¶
type Session = api.OpenedSession
type Sessions ¶
type Sessions interface {
LoadState(_ context.Context, _ api.None) ([]store.Project, error)
RecentProjects(_ context.Context, opts api.SearchTermOptions) ([]store.Recent, error)
AddProject(_ context.Context, opts api.AddProjectOptions) (api.None, error)
AddSessionFrom(_ context.Context, opts api.AddSessionFromOptions) (api.None, error)
SessionRefused(kind string) error
SandboxDefault(providerID, projectID, cwd string) bool
SetSessionSandbox(sessionID, sandbox string) error
SetSessionModel(sessionID, model string) error
SetSessionEffort(sessionID, effort string) error
SetSessionUltracode(sessionID string) error
SessionModel(sessionID string) string
SessionEffort(sessionID string) string
SessionUltracode(sessionID string) bool
SessionProfile(sessionID string) (string, error)
SetSessionProfile(sessionID, profile string) error
SetSessionSubagentDepth(sessionID string, depth int) error
SessionSubagentDepth(sessionID string) int
RenameSession(_ context.Context, opts api.LabelSessionOptions) (api.None, error)
SetSessionFolder(_ context.Context, opts api.SessionFolderOptions) (api.None, error)
RenameFolder(_ context.Context, opts api.RenameProjectFolderOptions) ([]string, error)
ColorFolder(projectID, folder, color string) ([]string, error)
SetSessionColor(_ context.Context, opts api.SessionColorOptions) (api.None, error)
DeleteSession(projectID, sessionID, activeID string) error
CloseSession(_ context.Context, opts api.ParkSessionOptions) (api.None, error)
CloseProject(_ context.Context, opts api.ProjectIDOptions) (api.None, error)
PurgeWorktreeSessions(_ context.Context, opts api.WorktreeSessionsOptions) (api.None, error)
}
Sessions is the persistence this needs: the workspace to resolve a project in, and the writes that add a session to one, take one away, and park one for a later resume. The store implements it.
type Terminal ¶
type Terminal interface {
Start(_ context.Context, opts api.StartOptions) (api.None, error)
// CloseBy closes the session on behalf of closerID, the session that asked
// for it, or empty when none did.
CloseBy(id, closerID string) error
// AgentName is the name that session's agent answers to in its provider's
// peer roster, read out of the provider's own record. Empty when there is
// none, which is what leaves `lich close` matching the derived name.
AgentName(id string) string
// SuggestProfile offers the person a profile for a session, moving nothing,
// and answers why it cannot apply when it cannot.
SuggestProfile(id, slug string) error
}
Terminal is the PTY side: the same spawn the window asks for when a card is first viewed, and the same close it asks for when a card goes away. The terminal service implements it.
type Worktrees ¶
type Worktrees interface {
Branch(path string) string
ListBranches(_ context.Context, opts api.PathOptions) (project.Branches, error)
ListCheckouts(_ context.Context, opts api.PathOptions) ([]project.Worktree, error)
CreateWorktree(_ context.Context, opts api.CreateWorktreeOptions) (*project.Worktree, error)
BaseFreshness(_ context.Context, opts api.PathBranchOptions) (project.BaseFreshness, error)
RemoveWorktree(_ context.Context, opts api.RemoveWorktreeOptions) (api.None, error)
WorktreeDirty(_ context.Context, opts api.PathOptions) (bool, error)
WorktreeAdopted(_ context.Context, opts api.PathOptions) (bool, error)
}
Worktrees is the git side. The project service implements it.
type WorktreesOptions ¶
type WorktreesOptions = api.WorktreesOptions