spawn

package
v0.75.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: AGPL-3.0 Imports: 16 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 ClosedEventName = "session-closed"

ClosedEventName carries a session closed outside the window, so the card goes away without a reload. Its payload is ClosedEvent.

View Source
const ColoredEventName = "sessions-colored"

ColoredEventName carries sessions painted outside the window, so their cards take the colour without a reload.

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

View Source
const FiledEventName = "sessions-filed"

FiledEventName carries sessions whose folder changed outside the window, so their cards move into or out of the folder's block without a reload. One event for a card filed and for a folder renamed, because the window's answer to both is the same: set these cards' folder.

View Source
const OpenedEventName = "session-opened"

OpenedEventName carries a session opened outside the window, so the card appears without a reload. Global, like every other session event: its consumer is the workspace state, which outlives any one card.

View Source
const ProjectOpenedEventName = "project-opened"

ProjectOpenedEventName carries a project opened outside the window — an agent naming a directory rather than a project already on screen — so its tab appears without a reload. Its payload is the whole store.Project, sessions included: a reopened project brings back the cards it was closed with, and the session this open is about to announce lands in a tab the window has to be holding already.

View Source
const RenamedEventName = "session-title"

RenamedEventName carries a session's new name, so every card takes it without a reload. The store emits it once store.RenameSession has written the row, whoever asked: the window, `lich rename` or the MCP tool.

It is the event the auto-applied ai-title already emits (terminal.titleEventName) rather than one of its own: the payload is the same pair and the window's answer to both is the same — relabel that card. A second name for one instruction would have bought a second handler saying it again.

Variables

This section is empty.

Functions

This section is empty.

Types

type Answered

type Answered struct {
	ID      string `json:"id"`
	Project string `json:"project"`
	Label   string `json:"label"`
	Answer  string `json:"answer"`
}

Answered is a session's answer to a side question.

type AskOptions

type AskOptions struct {
	From     string `json:"from"`
	Target   string `json:"target"`
	Project  string `json:"project"`
	Question string `json:"question"`
}

AskOptions is one Ask call, an object for the reason CloseOptions is one.

type Checkout

type Checkout struct {
	Name     string   `json:"name"`
	Path     string   `json:"path"`
	Dirty    bool     `json:"dirty"`
	Sessions []string `json:"sessions"`
}

Checkout is one of a project's git worktrees, as a caller outside the window needs to see it: what it is called, where it is, whether anything in it is uncommitted, and which sessions are open in it.

Sessions is what makes the list actionable rather than informational. A checkout with none is one nobody is working in; the last session in one is the thing whose closing decides whether the checkout stays (see Close).

type CloseOptions

type CloseOptions struct {
	From     string `json:"from"`
	Target   string `json:"target"`
	Project  string `json:"project"`
	Worktree string `json:"worktree"`
	Force    bool   `json:"force"`
}

CloseOptions is one Close call, an object for the reason OpenOptions is one: an option a newer lich adds is dropped by an older backend, and one an older lich does not send is its zero value.

type Closed

type Closed struct {
	ID        string `json:"id"`
	ProjectID string `json:"projectId"`
	Project   string `json:"project"`
	Label     string `json:"label"`
	// Worktree is the checkout the session lived in, empty for a session in the
	// project's own directory.
	Worktree string `json:"worktree"`
	// Kept is the answer given to the worktree question: the checkout was left on
	// disk rather than removed. Meaningless unless the close had that question to
	// answer — a session sharing its checkout, or living in the project's own
	// directory, reports false while leaving everything where it was.
	//
	// Nothing here reports the park, because every close but a removal parks and
	// a field that is true on all of them tells a caller nothing.
	Kept bool `json:"kept"`
	// Removed is whether the checkout was deleted.
	Removed bool `json:"removed"`
}

Closed is what a caller is told about the session it closed.

type ClosedEvent

type ClosedEvent struct {
	ID        string `json:"id"`
	ProjectID string `json:"projectId"`
	ActiveID  string `json:"activeId"`
}

ClosedEvent is the payload of ClosedEventName: which session left, the project it left, and which of that project's sessions is active now — the window has no say in the choice, because the row that records it is already written.

type ColorFolderOptions

type ColorFolderOptions struct {
	From    string `json:"from"`
	Project string `json:"project"`
	Folder  string `json:"folder"`
	Color   string `json:"color"`
}

ColorFolderOptions is one ColorFolder call, an object for the reason CloseOptions is one.

type Colored

type Colored struct {
	Project  string   `json:"project"`
	Folder   string   `json:"folder"`
	Color    string   `json:"color"`
	Sessions []string `json:"sessions"`
}

Colored is what a caller is told about a folder it painted: the sessions that took the colour, by label.

type ColoredEvent

type ColoredEvent struct {
	ProjectID string   `json:"projectId"`
	IDs       []string `json:"ids"`
	Color     string   `json:"color"`
}

ColoredEvent is ColoredEventName's payload. Color is "" when the cards went back to the theme.

type ControlOptions

type ControlOptions struct {
	From    string `json:"from"`
	Target  string `json:"target"`
	Project string `json:"project"`
	Action  string `json:"action"`
	Value   string `json:"value"`
	Args    string `json:"args"`
}

ControlOptions is one Control call, an object for the reason CloseOptions is one.

type Controlled

type Controlled struct {
	ID        string `json:"id"`
	Project   string `json:"project"`
	Label     string `json:"label"`
	Action    string `json:"action"`
	Value     string `json:"value,omitempty"`
	CommandID string `json:"command_id"`
	State     string `json:"state"`
}

Controlled is what a caller is told about a command it gave a session. Value is the prompt, model, effort or slash command's name the action carried, and CommandID the id the session's mod acks it under.

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 struct {
	From    string `json:"from"`
	Target  string `json:"target"`
	Project string `json:"project"`
	Folder  string `json:"folder"`
}

FileOptions is one File call, an object for the reason CloseOptions is one.

type Filed

type Filed struct {
	ID       string `json:"id"`
	Project  string `json:"project"`
	Label    string `json:"label"`
	Folder   string `json:"folder"`
	Previous string `json:"previous"`
}

Filed is what a caller is told about the session it filed. Previous is the folder it left, "" when it was in none.

type FiledEvent

type FiledEvent struct {
	ProjectID string   `json:"projectId"`
	IDs       []string `json:"ids"`
	Folder    string   `json:"folder"`
}

FiledEvent is FiledEventName's payload. Folder is "" when the cards were taken out of the one they were in.

type Folder

type Folder struct {
	Name     string   `json:"name"`
	Sessions []string `json:"sessions"`
}

Folder is one of a project's folders as a caller outside the window sees it: its name, which is its whole identity (store.SetSessionFolder), and the sessions filed under it, by label.

type FoldersOptions

type FoldersOptions struct {
	From    string `json:"from"`
	Project string `json:"project"`
}

FoldersOptions is one Folders call, an object for the reason CloseOptions is one.

type OpenOptions

type OpenOptions struct {
	From      string `json:"from"`
	Project   string `json:"project"`
	Kind      string `json:"kind"`
	Worktree  string `json:"worktree"`
	Base      string `json:"base"`
	Model     string `json:"model"`
	Effort    string `json:"effort"`
	Folder    string `json:"folder"`
	Ultracode bool   `json:"ultracode"`
}

OpenOptions is one Open call. It travels as a single JSON object rather than as positional arguments because a package manager replaces the lich binary under a running backend: a newer CLI's option the backend does not know is dropped, and one an older CLI does not send is its zero value, where a positional list refuses both on its count.

From is the session the caller runs in, empty for the command line run outside one. It decides two defaults: the project the session lands in and the provider it runs, both taken from the caller unless named. A caller with no session of its own must name the project — there is nothing to inherit. It is also recorded on the row as the new session's origin, which outlives the request that created it: the card says where it came from for as long as it exists.

Project is a project already on screen, by name, or a directory path — and a path is the only way to reach a project the window is not holding, which is what opening one from the command line means. See ensureProject.

Worktree, when given, is the branch name of a new git worktree created off Base (the project's current branch when Base is empty); the session is rooted there and labelled after it, exactly as the window's own worktree flow does. A Base without a Worktree has nothing to branch, and is refused rather than ignored.

Model, when given, is the model the provider is spawned on, in that provider's own spelling. It is recorded on the row so every later spawn repeats it. Effort, when given, is the reasoning effort, passed and recorded the same way. Ultracode turns Claude Code's ultracode on, at whatever effort the session runs; it is recorded the same way, and refused for every other provider.

Folder, when given, is the folder the session is filed under from its first frame: it is written with the row, so the card arrives in the folder's block rather than among its checkout's cards and then jumps.

type OpenSubagentOptions

type OpenSubagentOptions struct {
	From      string `json:"from"`
	Kind      string `json:"kind"`
	Worktree  string `json:"worktree"`
	Base      string `json:"base"`
	Model     string `json:"model"`
	Effort    string `json:"effort"`
	Ultracode bool   `json:"ultracode"`
}

OpenSubagentOptions is one OpenSubagent call, an object for the reason OpenOptions is one. Its fields mean what Open's do; there is no project or folder, both of which are the caller's.

type Refiled

type Refiled struct {
	Project  string   `json:"project"`
	From     string   `json:"from"`
	To       string   `json:"to"`
	Sessions []string `json:"sessions"`
}

Refiled is what a caller is told about a folder it renamed or took apart: every session that moved, which is the part a merge into an existing folder would otherwise hide.

type RenameFolderOptions

type RenameFolderOptions struct {
	From    string `json:"from"`
	Project string `json:"project"`
	Folder  string `json:"folder"`
	To      string `json:"to"`
}

RenameFolderOptions is one RenameFolder call, an object for the reason CloseOptions is one. Folder is the name to change and To the new one.

type RenameOptions

type RenameOptions struct {
	From    string `json:"from"`
	Target  string `json:"target"`
	Project string `json:"project"`
	Label   string `json:"label"`
}

RenameOptions is one Rename call, an object for the reason CloseOptions is one.

type Renamed

type Renamed struct {
	ID       string `json:"id"`
	Project  string `json:"project"`
	Label    string `json:"label"`
	Previous string `json:"previous"`
}

Renamed is what a caller is told about the session it renamed. Previous is the name that is gone, which is the half the caller cannot look up afterwards.

type RenamedEvent

type RenamedEvent struct {
	ID    string `json:"id"`
	Label string `json:"label"`
}

RenamedEvent is RenamedEventName's payload, the shape terminal's title event writes.

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(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(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(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(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(opts OpenOptions) (Session, error)

Open creates a session and starts its PTY.

func (*Service) OpenSubagent

func (s *Service) OpenSubagent(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(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(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(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 struct {
	ID              string `json:"id"`
	ProjectID       string `json:"projectId"`
	Project         string `json:"project"`
	Label           string `json:"label"`
	Name            string `json:"name"`
	Kind            string `json:"kind"`
	Path            string `json:"path"`
	NextSeq         int    `json:"nextSeq"`
	OriginSessionID string `json:"originSessionId"`
	OriginLabel     string `json:"originLabel"`
	Confined        bool   `json:"confined"`
	Folder          string `json:"folder"`
}

Session is one opened session: what the caller is told, and what the window is handed to draw the card. Both read the same struct because both need the same facts — a divergence between them is a card that does not match the session the agent was given.

Name is what the session answers to in Claude Code's peer roster; Label is the name on its card. Both address it (see internal/relay). Path is the worktree the session lives in, empty for a session in the project's own directory — the store's own spelling. NextSeq is the project's label counter after this session took its number. OriginSessionID and OriginLabel name the session that asked for this one — empty when the caller was not a session at all. Confined is whether the session opened inside the sandbox. Nobody is at this end to be asked, so it is the rung's own answer (store.SandboxDefault), and saying it is what keeps a caller from discovering the empty home and the read-only machine by running into them. Folder is the folder it was filed under, "" for none.

type Sessions

type Sessions interface {
	LoadState() ([]store.Project, error)
	RecentProjects(term string) ([]store.Recent, error)
	AddProject(id, name, path string) error
	AddSessionFrom(
		projectID, sessionID, label, kind, path string, nextSeq int, originID, originLabel, folder string,
	) 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(sessionID, label string) error
	SetSessionFolder(sessionID, folder string) error
	RenameFolder(projectID, from, to string) ([]string, error)
	ColorFolder(projectID, folder, color string) ([]string, error)
	DeleteSession(projectID, sessionID, activeID string) error
	CloseSession(projectID, sessionID, activeID string) error
	PurgeWorktreeSessions(projectID, path string) 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(id, projectID, cwd, kind, resume, name string, fork bool, cols, rows int) 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(path string) (project.Branches, error)
	ListCheckouts(path string) ([]project.Worktree, error)
	CreateWorktree(projectPath, projectID, name, base string, baseIsRemote bool) (*project.Worktree, error)
	BaseFreshness(path, branch string) (project.BaseFreshness, error)
	RemoveWorktree(projectPath, wtPath string, force, adoptedAck bool) error
	WorktreeDirty(wtPath string) (bool, error)
	WorktreeAdopted(wtPath string) bool
}

Worktrees is the git side. The project service implements it.

type WorktreesOptions

type WorktreesOptions struct {
	From    string `json:"from"`
	Project string `json:"project"`
}

WorktreesOptions is one Worktrees call, an object for the reason CloseOptions is one.

Jump to

Keyboard shortcuts

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