session

package
v0.3.15 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 27 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

Index

Constants

View Source
const (
	// StatusLineAuto routes it only for somebody who has a status line of
	// their own. It is the default, and is kept as nothing.
	StatusLineAuto = ""
	// StatusLineOn always does, at the cost of the footer's keyboard hints for
	// somebody who has no status line of their own.
	StatusLineOn = "on"
	// StatusLineOff never does.
	StatusLineOff = "off"
)

When a Claude pane's status line goes through Flockdeck.

View Source
const KindClaude = KindAgent

KindClaude is what an agent pane was called while the `claude` CLI was the only agent Flockdeck could run. It is the same kind under its older name, kept so that a pane is not read as a different sort of thing depending on which name the caller reached for.

View Source
const NoPrompt = transcript.NoPrompt

NoPrompt stands in for the summary of a conversation that says nothing about itself.

Variables

View Source
var GitBash = func() string { return gitBashFor(runtime.GOOS, os.Getenv, fileExists, exec.LookPath) }

GitBash returns the Git Bash Claude Code would run a command line through on Windows, or "" when it would find none and use PowerShell. It is a variable so a test can say which.

Functions

func ChatExe added in v0.2.3

func ChatExe(selfExe string) string

ChatExe returns the program an API agent's pane runs: Flockdeck's own binary, or on Windows the console build shipped beside it.

A release for Windows is linked as a GUI program (-H=windowsgui), so that starting it opens no console window, and Windows attaches no console to a GUI program even inside a pseudo-console: `flockdeck.exe chat` in a pane printed nothing and read nothing. A build without the twin -- a plain `go build` makes a console program -- runs itself as before.

func ClaudeArgs

func ClaudeArgs(sessionID, settingsPath string, resume bool, extra []string) []string

ClaudeArgs builds the argv for a Claude pane.

A pane is built from a Spec by Launch, and what this is for is to say, in one place and independently of the catalog, exactly what a Claude pane is run with. TestArgvFromSpecMatchesClaude holds the Spec-built argv against it, because a Claude pane that gains or loses an argument either refuses to start or starts a conversation that cannot be resumed.

New panes pin their conversation to our own session id with --session-id so that a later run can reattach to exactly that conversation with --resume, which is what makes layout restore meaningful rather than cosmetic.

func ClaudeShellCommand added in v0.3.0

func ClaudeShellCommand(line string) *exec.Cmd

ClaudeShellCommand is a command line run the way Claude Code runs a hook or a status line written as one: through the same shell, with the same flags and the same rewriting of the line for that shell.

Its window is hidden, as Claude Code hides the shell it starts: the output goes back to Claude Code through the bridge, and a console window would only flash up on every refresh.

func ConversationExists

func ConversationExists(sessionID string) bool

ConversationExists reports whether Claude Code has a stored transcript for a session id.

This matters because `claude --resume <id>` fails outright when there is nothing to resume: it prints "No conversation found with session ID" and exits. A pane that was created but never prompted has no transcript, so resuming one would kill it on restart, and would kill every such pane when a saved layout is restored.

func Env

func Env(extra ...string) []string

Env builds the environment for a pane: Flockdeck's own environment minus the markers of the session it was launched from, plus extra KEY=VALUE entries.

func EnvStripping

func EnvStripping(strip []string, extra ...string) []string

EnvStripping is Env with more variables to remove named explicitly, which is how a pane's environment comes to be driven by Spec.StripEnv rather than by what Flockdeck happened to know about Claude.

The built-in markers go whatever the list says. What a caller has is the catalog's union, and an agents.json entry for claude that gives a stripEnv of its own replaces the built-in one rather than adding to it: handed that union alone, every pane opened from inside Claude Code would believe it was a nested child session again.

An extra entry replaces an inherited one of the same name rather than joining it. A duplicated name in an environment block is resolved by the first copy, on Windows and on Unix alike, so appending alone would leave the stale value in force -- which is how a pane opened from inside another instance would tell its agent it was the pane that spawned it.

func InheritTrust

func InheritTrust(from, to string) error

InheritTrust records that a directory is trusted, given that another already is. It refuses when the source is not itself trusted, so this can only ever carry an answer the user has already given.

The file is read, changed and written back whole, and Claude Code saves it too -- busily so while a fan-out is starting agents -- so all of that happens under the lock Claude Code takes for its own saves. Without it a save landing between the read and the write here was thrown away by the write: another project's settings, the user's history.

func InheritTrustFor

func InheritTrustFor(spec agent.Spec, from, to string) error

InheritTrustFor carries an agent's answer for one directory to another, for an agent that has an answer to carry. For one that does not it is a no-op rather than an error: nothing was asked, so nothing has to be arranged.

func IsIdleReminder added in v0.3.7

func IsIdleReminder(event, notificationType string) bool

IsIdleReminder reports whether a lifecycle event is Claude Code's idle nudge rather than a real ask. Only a Notification carries a notification type at all, so every other event -- including PreToolUse's AskUserQuestion and the PermissionRequest event itself, both of which StatusForEvent already maps to StatusWaiting -- answers false here and goes on turning a pane amber regardless of who, if anyone, is watching it.

func IsTrusted

func IsTrusted(dir string) bool

IsTrusted reports whether Claude Code would treat this directory as trusted, asking the question the way Claude Code itself does. That is not only the directory's own answer: Claude Code first looks under the project the directory belongs to -- the repository root, or for a linked worktree the root of the repository it was made from -- and then walks up from the directory to its repository root, or to the top of the disk outside a repository, accepting an answer given for any folder on the way. Asking only about the exact directory reported a subfolder of a trusted repository, and every worktree of one, as untrusted, and fan-out then refused to carry over an answer the user had in fact given.

These rules were read from Claude Code 2.1.269: its trust check (nO), the key it files a project under (Pb and Dqe) and its walk up the folders (DH and NH). A later version that changes them changes what this has to do.

This is the answer that decides whether InheritTrust writes trust into Claude Code's configuration, so it fails safe: anything that cannot be read, followed or matched exactly counts for nothing, which can only ever make a directory less trusted than Claude Code would find it, never more. A directory on another machine is reported untrusted without being looked at.

func Look

func Look(spec agent.Spec) (string, error)

Look resolves the program an agent runs, or explains that it is not installed. The explanation carries the Spec's own Install line, because the answer to "codex is not on PATH" is wherever Codex comes from, and only the catalog knows that.

An API agent has nothing to find: it runs Flockdeck's own binary, which is already running.

func LookClaude

func LookClaude() (string, error)

LookClaude returns the path to the claude CLI, or an error explaining that it is not installed. Look does the same for any agent, reading the command and where it comes from off its Spec; this is the answer for the one agent Flockdeck could run before it had a Spec to read it from.

func RecentReplies

func RecentReplies(sessionID string, maxTurns int) []string

RecentReplies returns what a pane's agent said in its last few turns, newest first and at most maxTurns of them: Claude Code's, or for an API pane Flockdeck's own chat client's.

This is the text the agent actually wrote, read from its own transcript. Reading it off the terminal instead means reading a redrawn interface: bullets wrapped to the pane's width and so cut mid-sentence, spinner frames and token counters sitting exactly where a list item would be, and thinking the agent was only musing with. A plan is markdown, and the transcript is where the markdown is.

func Settings

func Settings(spec agent.Spec, dir, sessionID, selfExe, endpoint string) (string, error)

Settings writes the settings file an agent is handed so that it reports its lifecycle to Flockdeck, and returns its path.

An agent that reports no lifecycle gets none. Neither does one that reports its own without being handed a file to say where -- `flockdeck chat` is told in its environment -- because the path is only ever used as an argument, and writing one anyway would leave a file in the state directory for every pane ever opened that nothing would read.

func ShellArgs

func ShellArgs() []string

ShellArgs returns the argv for a plain shell pane on this platform.

func StripEnvUnion

func StripEnvUnion(specs []agent.Spec) []string

StripEnvUnion collects what to take out of a pane's environment across a whole catalog of agents.

The union rather than one Spec's list, because the markers being removed are those of the session Flockdeck itself was launched from, and that session's agent has nothing to do with the one about to run in the pane: a pane opened from inside Claude Code must not tell Codex it is a nested Claude session either.

func TrustedFor

func TrustedFor(spec agent.Spec, dir string) bool

TrustedFor reports whether the trust question an agent asks has already been answered for a directory.

An agent with no such question is reported as trusted, because there is nothing there to stop it: the point of asking is to know whether a fan-out would strand its children on a dialog, and an agent that never shows one never will.

func WantsSettings added in v0.3.4

func WantsSettings(spec agent.Spec) bool

WantsSettings reports whether an agent's arguments ever refer to a settings file -- Claude Code's do, and `flockdeck chat`'s, which is told where to report in its environment, do not.

func WriteHookSettings

func WriteHookSettings(dir, sessionID, selfExe, endpoint string) (string, error)

WriteHookSettings writes a settings file that makes the pane report its lifecycle to Flockdeck, and returns its path.

The hook command re-invokes this same binary in `hook` mode, so there is no dependency on node, python or a shell script living next to the binary. The settings are additive: the user's own settings and hooks still load.

Which events it subscribes to depends on the Claude Code that will read it, and this asks the claude on PATH; WriteHookSettingsFor asks the program the pane will actually run.

The shared secret is not written into the commands. A command's arguments are there for any user of the machine to read, for as long as it runs, in ps or /proc -- and a hook runs for every tool call of every pane. The pane already carries the secret in its environment, as FLOCKDECK_TOKEN, and the hook and the status line inherit it from Claude Code, which is where they read it; an environment is readable by its owner alone.

func WriteHookSettingsFor added in v0.2.3

func WriteHookSettingsFor(exe, dir, sessionID, selfExe, endpoint string) (string, error)

WriteHookSettingsFor is WriteHookSettings for a pane running a given Claude Code program -- the one its Spec names -- which is the one asked what events it has. An empty program is the claude on PATH.

func WriteHookSettingsWith added in v0.3.0

func WriteHookSettingsWith(exe, dir, sessionID, selfExe, endpoint string, sl StatusLine) (string, error)

WriteHookSettingsWith is WriteHookSettingsFor with the pane's status line routed through Flockdeck as sl says.

Types

type Config

type Config struct {
	ID   string // stable id; for agent panes this is also the conversation UUID
	Kind Kind
	// Spec is the agent running in the pane, and what the pane is understood
	// through after it starts: whether its status is reported to Flockdeck or has
	// to be read out of what it prints, and what to look for when it does.
	// A shell pane leaves it empty.
	Spec agent.Spec
	Name string // display name, usually the basename of Cwd
	Cwd  string
	Argv []string
	Env  []string
	Cols int
	Rows int
	// OnChange is installed as the session's OnChange before the process is
	// started. The reader starts with the process and can report a change
	// straight away, so assigning the field once Start has returned is a
	// write racing the reader's read of it.
	OnChange func()
}

Config describes a session to start.

type Conversation

type Conversation = transcript.Conversation

Conversation is a stored conversation that can be resumed.

func Conversations

func Conversations(cwd string) ([]Conversation, error)

Conversations lists the stored Claude Code conversations for a working directory, most recently used first.

type Kind

type Kind int

Kind distinguishes an agent pane from a plain shell pane.

const (
	// KindAgent is a pane running a coding agent, described by an agent.Spec.
	KindAgent Kind = iota
	// KindShell is a pane running the user's shell.
	KindShell
)

type Launch

type Launch struct {
	Spec  agent.Spec
	Model string
	// ID is the pane id, which doubles as the conversation id: pinning the two
	// together is what lets a later run reattach to exactly the conversation
	// this pane had rather than to whichever one the agent saw last.
	ID   string
	Name string
	Cwd  string
	// Prompt is the task the pane opens with. It goes into the argv and is
	// never typed into the terminal, because typing into a running interface
	// means guessing when it is ready to accept it.
	//
	// It is passed through even when resuming. Whether a resumed pane is given
	// a task again is the Spec's own answer, written in whether ResumeArgs
	// mentions the prompt, rather than something decided here for every agent.
	Prompt string
	// Resume asks for the conversation to be reattached rather than started
	// afresh. Whether that is possible -- the agent supports it, and its
	// transcript is there to be resumed -- is the caller's question to answer
	// before it gets here, because only the caller can read the transcript.
	Resume bool
	// SelfExe is Flockdeck's own binary: the hook command an agent calls back on,
	// and the program itself for an API runner, whose pane runs `flockdeck chat`.
	SelfExe string
	// SettingsDir is where the generated settings file is written, for an agent
	// whose arguments ask to be handed one.
	SettingsDir string
	// Endpoint is where a pane reports its lifecycle back to Flockdeck. The
	// secret it reports with is not written into the settings: it is
	// FLOCKDECK_TOKEN, in Env, which the hooks inherit.
	Endpoint string
	// StripEnv is what to take out of the inherited environment. The caller
	// passes the union across the whole catalog rather than this Spec's own
	// list, so that a pane is a clean top-level session whatever is running in
	// it: the markers of the Claude session Flockdeck was launched from have to go
	// from a Codex pane too. The markers Flockdeck knew before agents had Specs
	// are stripped whatever it holds.
	StripEnv []string
	// Env is added to the pane's environment last and wins over everything
	// before it: these are the FLOCKDECK_* variables telling the pane what to call
	// back on, which nothing in a catalog entry may shadow. The agent and model
	// are added alongside them without being asked for.
	Env        []string
	Cols, Rows int
}

Launch is a pane about to be started: which agent to run in it, which model it was asked for, and everything about the pane that the agent's argument list and environment are written in terms of.

It exists so that the knowledge of how a pane is started lives on the Spec rather than in the caller. Every decision Flockdeck once made in line for the `claude` CLI -- which program, which flags, whether a settings file is written and handed over, what is taken out of the environment -- is read off the Spec here, so that a second agent is a table entry rather than another branch at the point a pane is launched.

func (Launch) Config

func (l Launch) Config() (Config, error)

Config resolves a Launch into the configuration Start takes, writing the agent's settings file along the way if its arguments ask for one.

type Session

type Session struct {
	ID   string
	Kind Kind
	Cwd  string

	// OnChange is invoked (often from a reader goroutine) whenever the session
	// changes state. It must be cheap and must not call back into the session.
	// It is set through Config.OnChange: the reader is already running by the
	// time Start returns.
	OnChange func()
	// contains filtered or unexported fields
}

Session is a single pane: a process attached to a PTY.

Terminal emulation happens in the browser, so this type does not interpret the byte stream. It owns the process, fans its output out to viewers, keeps enough history to redraw a reconnecting one, and tracks what the agent is doing.

func Start

func Start(cfg Config) (*Session, error)

Start spawns the configured process attached to a new PTY and begins forwarding its output.

func (*Session) AltScreen added in v0.2.3

func (s *Session) AltScreen() bool

AltScreen reports whether the pane's program has switched to the alternate screen -- a full-screen program: vim, htop, an agent's own full-screen view -- and not back. The replay of one is its screen as it was drawn, a piece at a time, so a window starting afresh on it has to have it redrawn.

func (*Session) BracketedPaste added in v0.2.10

func (s *Session) BracketedPaste() bool

BracketedPaste reports whether the pane's program has asked for bracketed paste (mode 2004) and not switched it back off: whether text it is sent between ESC[200~ and ESC[201~ is taken as one paste, line breaks and all, rather than typed a key at a time with each break pressing Enter.

func (*Session) Close

func (s *Session) Close() error

Close terminates the process, and what it started, and releases the PTY. It is safe to call on a pane that has already exited, which releases the PTY on its own.

It returns once the process is gone, or after closeGrace. On Windows killing a process only begins its end, and until that is over it holds its working directory open: removing a pane's folder straight after closing the pane -- a worktree, a temporary checkout -- failed as "being used by another process" in four runs out of ten.

func (*Session) Epoch added in v0.2.3

func (s *Session) Epoch() int64

Epoch names this run of the pane's process. A restarted pane is a new session with output of its own, so a place in the last one's means nothing in it. When it started is what tells the two apart, and unlike a counter it still does across Flockdeck itself being restarted.

func (*Session) ExitErr

func (s *Session) ExitErr() error

ExitErr returns the process exit error once the session has exited.

func (*Session) Exited

func (s *Session) Exited() bool

Exited reports whether the process has terminated.

func (*Session) Name

func (s *Session) Name() string

Name returns the pane's display name.

func (*Session) Pid

func (s *Session) Pid() int

Pid returns the process id of the pane's process, or zero when it has none.

func (*Session) RecentText

func (s *Session) RecentText(maxBytes int) string

RecentText returns the tail of the pane's output as plain text, with escape sequences removed. It is what the fan-out dialog reads to find the work a lead agent has proposed.

func (*Session) Resize

func (s *Session) Resize(cols, rows int)

Resize resizes the PTY, clamped to something a terminal could plausibly be. Unchanged dimensions are skipped, since a resize forces applications to redraw.

func (*Session) SetName

func (s *Session) SetName(n string)

SetName sets the pane's display name.

func (*Session) SetStatus

func (s *Session) SetStatus(st Status, detail string)

SetStatus records an authoritative status transition, normally from a lifecycle hook. detail is an optional short label such as the running tool.

func (*Session) SetStatusFull added in v0.3.9

func (s *Session) SetStatusFull(st Status, detail, toolInput string)

SetStatusFull is SetStatus, additionally recording a lifecycle event's own ToolInput (see hooks.Event) alongside detail -- what a permission prompt or an AskUserQuestion call is actually asking, carried forward the same way detail is, and read back by ToolInput.

func (*Session) Size

func (s *Session) Size() (cols, rows int)

Size returns the current PTY dimensions.

func (*Session) Status

func (s *Session) Status() (Status, string)

Status returns the current status and its detail label.

func (*Session) StatusSince

func (s *Session) StatusSince() time.Time

StatusSince reports when the current status began, which is how long an agent has been waiting on you.

func (*Session) Subscribe

func (s *Session) Subscribe() (id int, replay []byte, out <-chan []byte)

Subscribe registers a viewer. It returns the output produced so far, so the viewer can rebuild its screen, and a channel of subsequent output. The channel is closed when the process exits or the viewer falls too far behind.

Unsubscribe must be called with the returned id when the viewer goes away.

func (*Session) SubscribeFrom added in v0.2.3

func (s *Session) SubscribeFrom(epoch, off int64) (id int, replay []byte, start int64, resumed bool, out <-chan []byte)

SubscribeFrom is Subscribe for a viewer that already holds the start of this session's output: the first off bytes of the run named by epoch. While the rest is still held, replay is only what the viewer is missing and resumed is true, so it can carry on with the screen and the scrollback it has. Otherwise replay is the usual one, for a terminal started afresh. The viewer counts the bytes of replay and of everything after it on from start, and that count is the off it gives when it next subscribes.

func (*Session) ToolInput added in v0.3.9

func (s *Session) ToolInput() string

ToolInput returns the ToolInput carried by the lifecycle event behind the current status and detail -- compact JSON in the shape hooks.buildToolInput produces, or "" when there is none (an agent that reports no lifecycle, or a wait that named no tool and none came before it either).

func (*Session) Unsubscribe

func (s *Session) Unsubscribe(id int)

Unsubscribe removes a viewer.

func (*Session) Usage

func (s *Session) Usage() Usage

Usage reports what this pane's process tree is costing the machine.

It reads the operating system rather than returning something already gathered, so it must not be called per keystroke or per chunk of output; it is meant for the periodic refresh that also updates status and branch labels. Repeated calls between two readings of the shared process table cost nothing and give the same answer.

func (*Session) Write

func (s *Session) Write(p []byte) (int, error)

Write sends input to the process. Writing to a pane whose process has ended reports that, rather than the closed-handle error the PTY would give, which says nothing about which pane or why.

func (*Session) WriteString

func (s *Session) WriteString(text string) error

WriteString sends text to the process.

type Status

type Status int

Status describes what a session is currently doing. An agent whose Spec says it reports a lifecycle drives this from the events it sends Flockdeck; for a shell, for an agent that reports nothing, and until the first event of one that does arrives, it is read out of the pane's output instead -- the terminal bell, how long it has been quiet, and the lines the Spec says the agent prints when it is blocked on you or back at its prompt.

const (
	// StatusStarting means the process has been spawned but has not yet
	// reported any lifecycle event.
	StatusStarting Status = iota
	// StatusWorking means the agent is actively producing output or running
	// tools, and does not need you.
	StatusWorking
	// StatusWaiting means the agent is blocked on you: a permission prompt, a
	// question, or an idle nudge. This is the status worth surfacing loudly.
	StatusWaiting
	// StatusIdle means the agent finished its turn and is waiting for a new
	// prompt.
	StatusIdle
	// StatusExited means the process is gone.
	StatusExited
)

func StatusForEvent

func StatusForEvent(event, tool string) (Status, string, bool)

StatusForEvent maps a Claude lifecycle event to the pane status it implies. The second return value is false for events that should not change status.

func (Status) NeedsAttention

func (s Status) NeedsAttention() bool

NeedsAttention reports whether the session is blocked waiting on the user.

func (Status) String

func (s Status) String() string

String returns a short lowercase name for the status.

func (Status) Symbol

func (s Status) Symbol() string

Symbol returns the glyph used to represent the status in the tab bar and pane titles.

type StatusLine added in v0.3.0

type StatusLine struct {
	// Mode is one of the StatusLine constants.
	Mode string
	// Endpoint is where the bridge posts what the status line was given.
	Endpoint string
	// Cwd is where the pane runs, which is where Claude Code looks for the
	// project's own settings.
	Cwd string
	// Home is Claude Code's own folder as the pane will have it, which is where
	// it looks for the user's settings. A catalog entry that sets
	// CLAUDE_CONFIG_DIR -- the usual way to run a second account -- moves it
	// away from Flockdeck's own, and reading Flockdeck's carried the other
	// account's status line. Empty is Flockdeck's own.
	Home string
}

StatusLine is what a Claude pane's settings need to route its status line through Flockdeck.

type Usage

type Usage struct {
	// CPUPercent is the share of a single core, averaged over cpuWindow. A
	// pane using two cores flat out reads 200.
	CPUPercent float64
	// RSSBytes is resident memory across the tree.
	RSSBytes uint64
	// Procs is how many processes the reading covers.
	Procs int
	// Known is false when the platform cannot answer, or the pane has no
	// process, or nothing has been read yet. There is no figure to show then,
	// which is better than showing a wrong one.
	Known bool
}

Usage is what a pane is costing the machine: its process and everything that process has spawned, because the `claude` CLI does its work in children and a figure covering only the process Flockdeck started would be the wrong one.

Directories

Path Synopsis
Package transcript reads what agents said.
Package transcript reads what agents said.

Jump to

Keyboard shortcuts

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