spawn

package
v0.78.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: AGPL-3.0 Imports: 18 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 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

View Source
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.

View Source
const (
	ControlDone      = "done"
	ControlDelivered = terminal.ModDelivered
	ControlEnded     = terminal.ModEnded
)

How far a command Control handed a session got by the time it answered.

View Source
const AskWaitLimit = 90 * time.Second

AskWaitLimit is how long Ask waits for an answer: the 90 seconds an MCP call may block (cli.mcpMaxWait). A brief answer, which the mod asks for, takes seconds; one of thousands of words was measured at 109 and does not fit.

View Source
const (

	// ControlWaitLimit is the longest Control waits for a session to confirm.
	ControlWaitLimit = commandAckWait
)

A few seconds cover an abort, a model or an effort, which the mod applies the moment it collects them, and a prompt to an idle session; past that the session is busy and the answer is minutes away. A slash command runs only once the session is idle and then takes as long as it takes: the longer wait is for /compact on an idle session. Both stay under the 90 seconds an MCP call may block (cli.mcpMaxWait).

Variables

This section is empty.

Functions

This section is empty.

Types

type Answered

type Answered = api.SessionAnswer

type AskOptions

type AskOptions = api.AskSessionOptions

type Checkout

type Checkout = api.WorktreeCheckout

type CloseOptions

type CloseOptions = api.CloseSessionOptions

type Closed

type Closed = api.ClosedSession

type ClosedEvent

type ClosedEvent = api.SessionClosed

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

type Events interface {
	Emit(name string, data any)
}

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 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 New

func New(sessions Sessions, worktrees Worktrees, term Terminal, events Events) *Service

func (*Service) Ask

func (s *Service) Ask(ctx context.Context, opts AskOptions) (Answered, error)

Ask puts a side question to a running Claude Code session and waits for its answer. The session's mod answers it with a tool-less fork of the session's own conversation: the session's turn goes on undisturbed and the question never enters its transcript. Every way it ends without an answer is an error naming the session, a wait that runs out included: an answer that lands later is dropped.

func (*Service) Close

func (s *Service) Close(ctx context.Context, opts CloseOptions) (Closed, error)

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

func (s *Service) CloseFinishedWorker(id string) error

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) ColorFolder

func (s *Service) ColorFolder(_ context.Context, opts ColorFolderOptions) (Colored, error)

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 running Claude Code session one command through its mod (docs/hooks/mod-control.md) and waits a bounded time for the mod to confirm it. action is one of the mod's kinds, value what that kind carries, and args what follows a slash command's name. A wait that ends after the session took the command is not an error: State says it was delivered and still goes through, or that the session ended. A command the session never took is withdrawn and is the error, and so is an ack that says the command failed, each naming the session.

func (*Service) File

func (s *Service) File(ctx context.Context, opts FileOptions) (Filed, error)

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

func (s *Service) Folders(ctx context.Context, opts FoldersOptions) ([]Folder, error)

Folders lists a project's folders. Project names it; empty takes the caller's own, exactly as Worktrees does.

func (*Service) Open

func (s *Service) Open(_ context.Context, opts OpenOptions) (Session, error)

Open creates a session and starts its PTY.

func (*Service) OpenSubagent

func (s *Service) OpenSubagent(_ context.Context, opts OpenSubagentOptions) (Session, error)

OpenSubagent opens a session as the subagent of the session From runs in (`lich open --subagent`), the way lich-plugin's mod turns Claude Code's own subagent call into a card. 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) Rename

func (s *Service) Rename(ctx context.Context, opts RenameOptions) (Renamed, error)

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

func (s *Service) RenameFolder(ctx context.Context, opts RenameFolderOptions) (Refiled, error)

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) Worktrees

func (s *Service) Worktrees(ctx context.Context, opts WorktreesOptions) ([]Checkout, error)

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)
	SandboxDefault(providerID, projectID, cwd string) bool
	SetSessionModel(sessionID, model string) error
	SetSessionEffort(sessionID, effort string) error
	SetSessionUltracode(sessionID 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)
	DeleteSession(projectID, sessionID, activeID string) error
	CloseSession(_ context.Context, opts api.ParkSessionOptions) (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
	// RunModCommand queues cmd for the session's Claude Code mod and waits,
	// until ctx ends, for the mod to confirm it.
	RunModCommand(ctx context.Context, id string, cmd terminal.ModCommand) (terminal.ModOutcome, 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

Jump to

Keyboard shortcuts

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