{"modulePath":"github.com/lichdotdev/lich","version":"v0.93.1","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package spawn\n\nPackage 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.\n\nIt 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).\n\nThe 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.\n\n## Constants\n\n```go\nconst (\n\t// KeepWorktree leaves the checkout on disk and parks the session's row, so\n\t// its conversation can be resumed later — the window's \"keep\" answer.\n\tKeepWorktree = \"keep\"\n\t// RemoveWorktree deletes the checkout and the session with it.\n\tRemoveWorktree = \"remove\"\n)\n```\nWhat 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.\n\n```go\nconst (\n\tControlDone      = \"done\"\n\tControlDelivered = terminal.ModDelivered\n\tControlEnded     = terminal.ModEnded\n)\n```\nHow far a command Control handed a session got by the time it answered.\n\n```go\nconst AskWaitLimit = 90 * time.Second\n```\nAskWaitLimit 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.\n\n```go\nconst (\n\n\t// ControlWaitLimit is the longest Control waits for a session to confirm.\n\tControlWaitLimit = commandAckWait\n)\n```\nA 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).\n\n## Types\n\n```go\ntype Answered = api.SessionAnswer\n```\n\n```go\ntype AskOptions = api.AskSessionOptions\n```\n\n```go\ntype Checkout = api.WorktreeCheckout\n```\n\n```go\ntype CloseOptions = api.CloseSessionOptions\n```\n\n```go\ntype CloseProjectOptions = api.CloseProjectOptions\n```\n\n```go\ntype Closed = api.ClosedSession\n```\n\n```go\ntype ClosedEvent = api.SessionClosed\n```\n\n```go\ntype ClosedProject = api.ClosedProject\n```\n\n```go\ntype ColorFolderOptions = api.ColorFolderOptions\n```\n\n```go\ntype Colored = api.ColoredFolder\n```\n\n```go\ntype ColoredEvent = api.SessionsColored\n```\n\n```go\ntype ControlOptions = api.ControlSessionOptions\n```\n\n```go\ntype Controlled = api.ControlledSession\n```\n\n```go\ntype Events interface {\n\tEmit(name string, data any)\n}\n```\nEvents 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.\n\n```go\ntype FileOptions = api.FileSessionOptions\n```\n\n```go\ntype Filed = api.FiledSession\n```\n\n```go\ntype FiledEvent = api.SessionsFiled\n```\n\n```go\ntype Folder = api.SessionFolder\n```\n\n```go\ntype FoldersOptions = api.FoldersOptions\n```\n\n```go\ntype HandoffOptions = api.HandoffSessionOptions\n```\n\n```go\ntype OpenOptions = api.OpenSessionOptions\n```\n\n```go\ntype OpenSubagentOptions = api.OpenSubagentOptions\n```\n\n```go\ntype Refiled = api.RenamedFolder\n```\n\n```go\ntype RenameFolderOptions = api.RenameFolderOptions\n```\n\n```go\ntype RenameOptions = api.RenameSessionOptions\n```\n\n```go\ntype Renamed = api.RenamedSession\n```\n\n```go\ntype RenamedEvent = api.SessionTitle\n```\n\n```go\ntype Service struct {\n\t// contains filtered or unexported fields\n}\n```\nService opens sessions on behalf of a caller outside the window.\n\n```go\nfunc New(sessions Sessions, worktrees Worktrees, term Terminal, events Events) *Service\n```\n\n```go\nfunc (s *Service) Ask(ctx context.Context, opts AskOptions) (Answered, error)\n```\nAsk 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.\n\n```go\nfunc (s *Service) Close(ctx context.Context, opts CloseOptions) (Closed, error)\n```\nClose closes a session, and settles what happens to the checkout it was the last one in.\n\nThe 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.\n\nA session that shares its checkout with another, or lives in the project's own directory, has nothing at stake and closes on the spot.\n\n```go\nfunc (s *Service) CloseFinishedWorker(id string) error\n```\nCloseFinishedWorker 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.\n\nA 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).\n\n```go\nfunc (s *Service) CloseProject(ctx context.Context, opts CloseProjectOptions) (ClosedProject, error)\n```\nCloseProject 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.\n\nThe 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.\n\nTwo 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).\n\n```go\nfunc (s *Service) ColorFolder(_ context.Context, opts ColorFolderOptions) (Colored, error)\n```\nColorFolder 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.\n\nThe 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.\n\n```go\nfunc (s *Service) Control(ctx context.Context, opts ControlOptions) (Controlled, error)\n```\nControl 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.\n\n```go\nfunc (s *Service) File(ctx context.Context, opts FileOptions) (Filed, error)\n```\nFile 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.\n\ntarget is the session to file, by either of the names it answers to; empty files the caller's own, as a rename does.\n\n```go\nfunc (s *Service) Folders(ctx context.Context, opts FoldersOptions) ([]Folder, error)\n```\nFolders lists a project's folders. Project names it; empty takes the caller's own, exactly as Worktrees does.\n\n```go\nfunc (s *Service) Handoff(ctx context.Context, opts HandoffOptions) (Session, error)\n```\nHandoff 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.\n\n```go\nfunc (s *Service) Open(ctx context.Context, opts OpenOptions) (Session, error)\n```\nOpen creates a session and starts its PTY.\n\n```go\nfunc (s *Service) OpenSubagent(ctx context.Context, opts OpenSubagentOptions) (Session, error)\n```\nOpenSubagent 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:\n\n  - 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;\n  - 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);\n  - 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).\n\nThe 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.\n\n```go\nfunc (s *Service) Project(ctx context.Context, opts api.ProjectRefOptions) (api.ProjectRef, error)\n```\nProject resolves an open project as every other spawn call does, for a caller that goes on to a method keyed by project id.\n\n```go\nfunc (s *Service) Rename(ctx context.Context, opts RenameOptions) (Renamed, error)\n```\nRename 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).\n\nTarget 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.\n\nA 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.\n\n```go\nfunc (s *Service) RenameFolder(ctx context.Context, opts RenameFolderOptions) (Refiled, error)\n```\nRenameFolder 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.\n\nThe 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.\n\n```go\nfunc (s *Service) Worktrees(ctx context.Context, opts WorktreesOptions) ([]Checkout, error)\n```\nWorktrees lists a project's checkouts. Project names it; empty takes the caller's own, exactly as Open does.\n\nThe 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.\n\n```go\ntype Session = api.OpenedSession\n```\n\n```go\ntype Sessions interface {\n\tLoadState(_ context.Context, _ api.None) ([]store.Project, error)\n\tRecentProjects(_ context.Context, opts api.SearchTermOptions) ([]store.Recent, error)\n\tAddProject(_ context.Context, opts api.AddProjectOptions) (api.None, error)\n\tAddSessionFrom(_ context.Context, opts api.AddSessionFromOptions) (api.None, error)\n\tSessionRefused(kind string) error\n\tSandboxDefault(providerID, projectID, cwd string) bool\n\tSetSessionSandbox(sessionID, sandbox string) error\n\tSetSessionModel(sessionID, model string) error\n\tSetSessionEffort(sessionID, effort string) error\n\tSetSessionUltracode(sessionID string) error\n\tSessionModel(sessionID string) string\n\tSessionEffort(sessionID string) string\n\tSessionUltracode(sessionID string) bool\n\tSessionProfile(sessionID string) (string, error)\n\tSetSessionProfile(sessionID, profile string) error\n\tSetSessionSubagentDepth(sessionID string, depth int) error\n\tSessionSubagentDepth(sessionID string) int\n\tRenameSession(_ context.Context, opts api.LabelSessionOptions) (api.None, error)\n\tSetSessionFolder(_ context.Context, opts api.SessionFolderOptions) (api.None, error)\n\tRenameFolder(_ context.Context, opts api.RenameProjectFolderOptions) ([]string, error)\n\tColorFolder(projectID, folder, color string) ([]string, error)\n\tSetSessionColor(_ context.Context, opts api.SessionColorOptions) (api.None, error)\n\tDeleteSession(projectID, sessionID, activeID string) error\n\tCloseSession(_ context.Context, opts api.ParkSessionOptions) (api.None, error)\n\tCloseProject(_ context.Context, opts api.ProjectIDOptions) (api.None, error)\n\tPurgeWorktreeSessions(_ context.Context, opts api.WorktreeSessionsOptions) (api.None, error)\n}\n```\nSessions 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.\n\n```go\ntype Terminal interface {\n\tStart(_ context.Context, opts api.StartOptions) (api.None, error)\n\t// CloseBy closes the session on behalf of closerID, the session that asked\n\t// for it, or empty when none did.\n\tCloseBy(id, closerID string) error\n\t// AgentName is the name that session's agent answers to in its provider's\n\t// peer roster, read out of the provider's own record. Empty when there is\n\t// none, which is what leaves `lich close` matching the derived name.\n\tAgentName(id string) string\n\t// SuggestProfile offers the person a profile for a session, moving nothing,\n\t// and answers why it cannot apply when it cannot.\n\tSuggestProfile(id, slug string) error\n\t// RunModCommand queues cmd for the session's Claude Code mod and waits,\n\t// until ctx ends, for the mod to confirm it.\n\tRunModCommand(ctx context.Context, id string, cmd terminal.ModCommand) (terminal.ModOutcome, error)\n}\n```\nTerminal 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.\n\n```go\ntype Worktrees interface {\n\tBranch(path string) string\n\tListBranches(_ context.Context, opts api.PathOptions) (project.Branches, error)\n\tListCheckouts(_ context.Context, opts api.PathOptions) ([]project.Worktree, error)\n\tCreateWorktree(_ context.Context, opts api.CreateWorktreeOptions) (*project.Worktree, error)\n\tBaseFreshness(_ context.Context, opts api.PathBranchOptions) (project.BaseFreshness, error)\n\tRemoveWorktree(_ context.Context, opts api.RemoveWorktreeOptions) (api.None, error)\n\tWorktreeDirty(_ context.Context, opts api.PathOptions) (bool, error)\n\tWorktreeAdopted(_ context.Context, opts api.PathOptions) (bool, error)\n}\n```\nWorktrees is the git side. The project service implements it.\n\n```go\ntype WorktreesOptions = api.WorktreesOptions\n```\n\n","path":"github.com/lichdotdev/lich/internal/spawn","name":"spawn","synopsis":"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).","isRedistributable":true}
